Merge branch 'develop' into feature/form_validation/custom_error_per_field
diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php
index 852fc71..9efa9f3 100644
--- a/system/libraries/Form_validation.php
+++ b/system/libraries/Form_validation.php
@@ -144,14 +144,16 @@
 	 * Set Rules
 	 *
 	 * This function takes an array of field names and validation
-	 * rules as input, validates the info, and stores it
+	 * rules as input, any custom error messages, validates the info, 
+	 * and stores it
 	 *
 	 * @param	mixed	$field
 	 * @param	string	$label
 	 * @param	mixed	$rules
+	 * @param	array	$errors
 	 * @return	CI_Form_validation
 	 */
-	public function set_rules($field, $label = '', $rules = '')
+	public function set_rules($field, $label = '', $rules = '', $errors = array())
 	{
 		// No reason to set rules if we have no POST data
 		// or a validation array has not been specified
@@ -175,8 +177,11 @@
 				// If the field label wasn't passed we use the field name
 				$label = isset($row['label']) ? $row['label'] : $row['field'];
 
+				// Add the custom error message array
+				$errors = (isset($row['error_msg']) && is_array($row['error_msg'])) ? $row['error_msg'] : array();
+
 				// Here we go!
-				$this->set_rules($row['field'], $label, $row['rules']);
+				$this->set_rules($row['field'], $label, $row['rules'], $errors);
 			}
 
 			return $this;
@@ -224,6 +229,7 @@
 			'field'		=> $field,
 			'label'		=> $label,
 			'rules'		=> $rules,
+			'error_msg' => $errors,
 			'is_array'	=> $is_array,
 			'keys'		=> $indexes,
 			'postdata'	=> NULL,
@@ -602,7 +608,12 @@
 				// Set the message type
 				$type = in_array('required', $rules) ? 'required' : 'isset';
 
-				if (isset($this->_error_messages[$type]))
+				// Check if a custom message defined
+				if (isset($this->_field_data[$row['field']]['error_msg'][$type]))
+				{
+					$line = $this->_field_data[$row['field']]['error_msg'][$type];
+				}
+				elseif (isset($this->_error_messages[$type]))
 				{
 					$line = $this->_error_messages[$type];
 				}
@@ -746,7 +757,12 @@
 			// Did the rule test negatively? If so, grab the error.
 			if ($result === FALSE)
 			{
-				if ( ! isset($this->_error_messages[$rule]))
+				// Check if a custom message defined
+				if(isset($this->_field_data[$row['field']]['error_msg'][$rule]))
+				{
+					$line = $this->_field_data[$row['field']]['error_msg'][$rule];
+				}
+				elseif ( ! isset($this->_error_messages[$rule]))
 				{
 					if (FALSE === ($line = $this->CI->lang->line('form_validation_'.$rule))
 						// DEPRECATED support for non-prefixed keys
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index cbc1c66..314ae8a 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -314,6 +314,7 @@
       -  Added support for named parameters in error messages.
       -  :doc:`Language <libraries/language>` line keys must now be prefixed with **form_validation_**.
       -  Added rule **alpha_numeric_spaces**.
+      -  Added **support** for custom error messages **per field rule**.
 
    -  :doc:`Caching Library <libraries/caching>` changes include:
 
diff --git a/user_guide_src/source/libraries/form_validation.rst b/user_guide_src/source/libraries/form_validation.rst
index 8534175..00f7c41 100644
--- a/user_guide_src/source/libraries/form_validation.rst
+++ b/user_guide_src/source/libraries/form_validation.rst
@@ -200,6 +200,7 @@
    message. For example, if your field is named "user" you might give it
    a human name of "Username".
 #. The validation rules for this form field.
+#. (optional) Set custom error messages on any rules given for current field. If not provided will use the default one.
 
 .. note:: If you would like the field name to be stored in a language
 	file, please see :ref:`translating-field-names`.
@@ -225,7 +226,9 @@
 			$this->load->library('form_validation');
 
 			$this->form_validation->set_rules('username', 'Username', 'required');
-			$this->form_validation->set_rules('password', 'Password', 'required');
+			$this->form_validation->set_rules('password', 'Password', 'required',
+				array('required'=>'You must provide a %s.')
+			);
 			$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
 			$this->form_validation->set_rules('email', 'Email', 'required');
 
@@ -263,7 +266,10 @@
 		array(
 			'field' => 'password',
 			'label' => 'Password',
-			'rules' => 'required'
+			'rules' => 'required',
+			'error_msg' => array(
+				'required' => 'You must provide a %s.',
+			),
 		),
 		array(
 			'field' => 'passconf',
@@ -285,7 +291,14 @@
 CodeIgniter lets you pipe multiple rules together. Let's try it. Change
 your rules in the third parameter of rule setting method, like this::
 
-	$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[users.username]');
+	$this->form_validation->set_rules(
+		'username', 'Username',
+		'required|min_length[5]|max_length[12]|is_unique[users.username]',
+		array(
+			'required'    => 'You have not provided %s.',
+			'is_unique'  => 'This %s already exists.'
+		)
+	);
 	$this->form_validation->set_rules('password', 'Password', 'required');
 	$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[password]');
 	$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
@@ -469,11 +482,18 @@
 All of the native error messages are located in the following language
 file: **system/language/english/form_validation_lang.php**
 
-To set your own custom message you can either edit that file, or use the
-following method::
+To set your own global custom message for a rule, you can either 
+edit that file, or use the following method::
 
 	$this->form_validation->set_message('rule', 'Error Message');
 
+If you need to set a custom error message for a particular field on 
+some particular rule, use the set_rules() method::
+
+	$this->form_validation->set_rules('field_name', 'Field Label', 'rule1|rule2|rule3',
+		array('rule2'	=> 'Error Message on rule2 for this field_name')
+	);
+
 Where rule corresponds to the name of a particular rule, and Error
 Message is the text you would like displayed.
 
@@ -941,11 +961,12 @@
 $this->form_validation->set_rules()
 ===================================
 
-	.. php:method:: set_rules ($field, $label = '', $rules = '')
+	.. php:method:: set_rules ($field, $label = '', $rules = '', $errors = array())
 
 		:param string $field: The field name
 		:param string $label: The field label
 		:param mixed $rules: The rules, as a string with rules separated by a pipe "|", or an array or rules.
+		:param array $errors: Custom error messages
 		:rtype: Object
 
 		Permits you to set validation rules, as described in the tutorial