diff options
author | Eric Roberts <eric@cryode.com> | 2012-01-24 07:59:44 +0100 |
---|---|---|
committer | Eric Roberts <eric@cryode.com> | 2012-01-24 07:59:44 +0100 |
commit | 41cc0908918f48d948fe1e1fc9c74fdec58b7a60 (patch) | |
tree | 2cb94c8ae7e28f3ee4022e15e179268a72e9eb80 /system/libraries/Form_validation.php | |
parent | 96db8f91c34c18119548cacc4692362f51e70407 (diff) |
Better support for using field names and rule parameters in error messages.
Diffstat (limited to 'system/libraries/Form_validation.php')
-rw-r--r-- | system/libraries/Form_validation.php | 25 |
1 files changed, 23 insertions, 2 deletions
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 |