Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add support for per field/rule error messages to the Form Validation Library #1252

Closed
wants to merge 4 commits into
from
@@ -204,8 +204,12 @@ public function set_data($data = '')
* @param string
* @return object
*/
- public function set_message($lang, $val = '')
+ public function set_message($lang, $val = '', $name = FALSE)
{
+ if($name !== FALSE){
+ $lang = $name.'.'.$lang;
+ }
+
if ( ! is_array($lang))
{
$lang = array($lang => $val);
@@ -527,17 +531,21 @@ protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
// Set the message type
$type = in_array('required', $rules) ? 'required' : 'isset';
- if ( ! isset($this->_error_messages[$type]))
+ if ( ! isset($this->_error_messages[$type]) && ! isset($this->_error_messages[$row['field'].'.'.$type]))
{
if (FALSE === ($line = $this->CI->lang->line($type)))
{
$line = 'The field was not set';
}
}
- else
+ else if (! isset($this->_error_messages[$row['field'].'.'.$type]))
{
$line = $this->_error_messages[$type];
}
+ else
+ {
+ $line = $this->_error_messages[$row['field'].'.'.$type];
+ }
// Build the error message
$message = sprintf($line, $this->_translate_fieldname($row['label']));
@@ -667,17 +675,21 @@ protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
// Did the rule test negatively? If so, grab the error.
if ($result === FALSE)
{
- if ( ! isset($this->_error_messages[$rule]))
+ if ( ! isset($this->_error_messages[$rule]) && ! isset($this->_error_messages[$row['field'].'.'.$rule]))
{
if (FALSE === ($line = $this->CI->lang->line($rule)))
{
$line = 'Unable to access an error message corresponding to your field name.';
}
}
- else
+ elseif (! isset($this->_error_messages[$row['field'].'.'.$rule]))
{
$line = $this->_error_messages[$rule];
}
+ else
+ {
+ $line = $this->_error_messages[$row['field'].'.'.$rule];
+ }
// Is the parameter we are inserting into the error message the name
// of another field? If so we need to grab its "field label"