summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Roberts <eric@cryode.com>2012-01-24 07:59:44 +0100
committerEric Roberts <eric@cryode.com>2012-01-24 07:59:44 +0100
commit41cc0908918f48d948fe1e1fc9c74fdec58b7a60 (patch)
tree2cb94c8ae7e28f3ee4022e15e179268a72e9eb80
parent96db8f91c34c18119548cacc4692362f51e70407 (diff)
Better support for using field names and rule parameters in error messages.
-rw-r--r--system/language/english/form_validation_lang.php46
-rw-r--r--system/libraries/Form_validation.php25
-rw-r--r--user_guide_src/source/libraries/form_validation.rst22
3 files changed, 60 insertions, 33 deletions
diff --git a/system/language/english/form_validation_lang.php b/system/language/english/form_validation_lang.php
index 6afa37a29..a1e02045f 100644
--- a/system/language/english/form_validation_lang.php
+++ b/system/language/english/form_validation_lang.php
@@ -25,29 +25,29 @@
* @filesource
*/
-$lang['required'] = "The %s field is required.";
-$lang['isset'] = "The %s field must have a value.";
-$lang['valid_email'] = "The %s field must contain a valid email address.";
-$lang['valid_emails'] = "The %s field must contain all valid email addresses.";
-$lang['valid_url'] = "The %s field must contain a valid URL.";
-$lang['valid_ip'] = "The %s field must contain a valid IP.";
-$lang['min_length'] = "The %s field must be at least %s characters in length.";
-$lang['max_length'] = "The %s field cannot exceed %s characters in length.";
-$lang['exact_length'] = "The %s field must be exactly %s characters in length.";
-$lang['alpha'] = "The %s field may only contain alphabetical characters.";
-$lang['alpha_numeric'] = "The %s field may only contain alpha-numeric characters.";
-$lang['alpha_dash'] = "The %s field may only contain alpha-numeric characters, underscores, and dashes.";
-$lang['numeric'] = "The %s field must contain only numbers.";
-$lang['is_numeric'] = "The %s field must contain only numeric characters.";
-$lang['integer'] = "The %s field must contain an integer.";
-$lang['regex_match'] = "The %s field is not in the correct format.";
-$lang['matches'] = "The %s field does not match the %s field.";
-$lang['is_unique'] = "The %s field must contain a unique value.";
-$lang['is_natural'] = "The %s field must contain only positive numbers.";
-$lang['is_natural_no_zero'] = "The %s field must contain a number greater than zero.";
-$lang['decimal'] = "The %s field must contain a decimal number.";
-$lang['less_than'] = "The %s field must contain a number less than %s.";
-$lang['greater_than'] = "The %s field must contain a number greater than %s.";
+$lang['required'] = 'The {field} field is required.';
+$lang['isset'] = 'The {field} field must have a value.';
+$lang['valid_email'] = 'The {field} field must contain a valid email address.';
+$lang['valid_emails'] = 'The {field} field must contain all valid email addresses.';
+$lang['valid_url'] = 'The {field} field must contain a valid URL.';
+$lang['valid_ip'] = 'The {field} field must contain a valid IP.';
+$lang['min_length'] = 'The {field} field must be at least {param} characters in length.';
+$lang['max_length'] = 'The {field} field cannot exceed {param} characters in length.';
+$lang['exact_length'] = 'The {field} field must be exactly {param} characters in length.';
+$lang['alpha'] = 'The {field} field may only contain alphabetical characters.';
+$lang['alpha_numeric'] = 'The {field} field may only contain alpha-numeric characters.';
+$lang['alpha_dash'] = 'The {field} field may only contain alpha-numeric characters, underscores, and dashes.';
+$lang['numeric'] = 'The {field} field must contain only numbers.';
+$lang['is_numeric'] = 'The {field} field must contain only numeric characters.';
+$lang['integer'] = 'The {field} field must contain an integer.';
+$lang['regex_match'] = 'The {field} field is not in the correct format.';
+$lang['matches'] = 'The {field} field does not match the {param} field.';
+$lang['is_unique'] = 'The {field} field must contain a unique value.';
+$lang['is_natural'] = 'The {field} field must contain only positive numbers.';
+$lang['is_natural_no_zero'] = 'The {field} field must contain a number greater than zero.';
+$lang['decimal'] = 'The {field} field must contain a decimal number.';
+$lang['less_than'] = 'The {field} field must contain a number less than {param}.';
+$lang['greater_than'] = 'The {field} field must contain a number greater than {param}.';
/* End of file form_validation_lang.php */
diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php
index 0a6a2af0d..ebd96e402 100644
--- a/system/libraries/Form_validation.php
+++ b/system/libraries/Form_validation.php
@@ -503,7 +503,7 @@ class CI_Form_validation {
}
// Build the error message
- $message = sprintf($line, $this->_translate_fieldname($row['label']));
+ $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']));
// Save the error message
$this->_field_data[$row['field']]['error'] = $message;
@@ -651,7 +651,7 @@ class CI_Form_validation {
}
// Build the error message
- $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
+ $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param);
// Save the error message
$this->_field_data[$row['field']]['error'] = $message;
@@ -694,6 +694,27 @@ class CI_Form_validation {
}
// --------------------------------------------------------------------
+
+ /**
+ * Build an error message using the field and param.
+ *
+ * @param string The error message line
+ * @param string A field's human name
+ * @param mixed A rule's optional parameter
+ * @return string
+ */
+ protected function _build_error_msg($line, $field = '', $param = '')
+ {
+ // Check for %s in the string for legacy support.
+ if (strpos($line, '%s') !== false)
+ {
+ return sprintf($line, $field, $param);
+ }
+
+ return str_replace(array('{field}', '{param}'), array($field, $param), $line);
+ }
+
+ // --------------------------------------------------------------------
/**
* Get the value from a form
diff --git a/user_guide_src/source/libraries/form_validation.rst b/user_guide_src/source/libraries/form_validation.rst
index e7875bc22..bf06445f9 100644
--- a/user_guide_src/source/libraries/form_validation.rst
+++ b/user_guide_src/source/libraries/form_validation.rst
@@ -139,7 +139,6 @@ this code and save it to your applications/controllers/ folder::
}
}
}
- ?>
Try it!
=======
@@ -240,7 +239,6 @@ Your controller should now look like this::
}
}
}
- ?>
Now submit the form with the fields blank and you should see the error
messages. If you submit the form with all the fields populated you'll
@@ -443,7 +441,6 @@ Here's how your controller should now look::
}
}
- ?>
Reload your form and submit it with the word "test" as the username. You
can see that the form field data was passed to your callback function
@@ -466,7 +463,7 @@ Setting Error Messages
======================
All of the native error messages are located in the following language
-file: language/english/form_validation_lang.php
+file: system/language/english/form_validation_lang.php
To set your own custom message you can either edit that file, or use the
following function::
@@ -476,8 +473,18 @@ following function::
Where rule corresponds to the name of a particular rule, and Error
Message is the text you would like displayed.
-If you include %s in your error string, it will be replaced with the
-"human" name you used for your field when you set your rules.
+If you'd like to include a field's "human" name or the optional
+parameter some rules allow for (such as max_length), you can add the
+**{field}** and **{param}** tags to your message, respectively.
+
+ $this->form_validation->set_message('min_length', '{field} must have at least {param} characters.');
+
+On a field with the human name Username and a rule of min_length[5], an
+error would display: "Username must have at least 5 characters."
+
+.. note:: The old method of using **%s** in your error messages will
+still work, however it will override the tags above. You should use
+one or the other.
In the "callback" example above, the error message was set by passing
the name of the function::
@@ -571,7 +578,7 @@ Try it! Change your form so that it looks like this::
If there are no errors, nothing will be shown. If there is an error, the
message will appear.
-**Important Note:** If you use an array as the name of a form field, you
+.. note:: **Important Note:** If you use an array as the name of a form field, you
must supply it as an array to the function. Example::
<?php echo form_error('options[size]'); ?>
@@ -723,7 +730,6 @@ function named signup. Here's what your class might look like::
}
}
}
- ?>
In your validation config file, you will name your rule group
member/signup::