Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>CodeIgniter doesn't support multiple error messages for the same rule natively, but there's a couple of workarounds you may try:</p> <p>As @HashemQolami suggests, you can use multiple <a href="http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks" rel="nofollow">callback functions</a> and set a different error message for each one:</p> <pre><code>$this-&gt;form_validation-&gt;set_rules('txt_first_name', 'First Name', 'callback_numeric_a'); $this-&gt;form_validation-&gt;set_rules('txt_last_name', 'Last Name', 'callback_numeric_b'); </code></pre> <p>The drawback for this method is that obviously it's not modular but rather repetitive as you'd need to define multiple functions in your controller like this one</p> <pre><code>public function numeric_a($str){ $this-&gt;form_validation-&gt;set_message('numeric_a', 'a'); return $this-&gt;numeric($str); } </code></pre> <p>Another workaround I've used is set the message of the rule as <code>%s</code> in the language file and then setting the custom message as the label of the field</p> <pre><code>$lang['numeric'] = '%s'; $this-&gt;form_validation-&gt;set_rules('txt_first_name', 'a', 'numeric'); $this-&gt;form_validation-&gt;set_rules('txt_last_name', 'b', 'numeric'); </code></pre> <p>The drawback here is that it basically messes up the error messaging system since you'd have to define the label for each field and would only work correctly with one validation rule per field. Still I have found it useful in contact forms where you basically just need to validate the presence of some required fields.</p> <p>Now since I've found myself in need for a better implementation for this feature, your post inspired me to put together a simple extension to the form validation class, unfortunately I had to "hack" the main <code>execute</code> method since there's no special function for retrieving the error messages.</p> <p>I added a method <code>set_custom_message()</code> to set a custom message for a rule to a specific field or to an array of fields.</p> <pre><code>$this-&gt;form_validation-&gt;set_custom_message('txt_first_name','numeric','a'); $this-&gt;form_validation-&gt;set_custom_message('txt_last_name','numeric','b'); //Example passing an array of fields $this-&gt;form_validation-&gt;set_custom_message(array('txt_first_name','txt_last_name'),'numeric','c'); </code></pre> <p>Here's the code for the extended class in case someone else is interested:</p> <p>Note that this is based on the form validation class included in <strong>CodeIgniter v2.1.4</strong></p> <pre><code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * MY_Form_validation Class * * Extends Form_Validation library * * Adds custom message support. * */ class MY_Form_validation extends CI_Form_validation { protected $_custom_messages = array(); public function set_custom_message($field = '', $rule = '', $message = '' ){ if(is_array($field)){ foreach($field as $id){ $this-&gt;_custom_messages[$id][$rule] = $message; } return; } $this-&gt;_custom_messages[$field][$rule] = $message; return; } protected function _execute($row, $rules, $postdata = NULL, $cycles = 0) { // If the $_POST data is an array we will run a recursive call if (is_array($postdata)) { foreach ($postdata as $key =&gt; $val) { $this-&gt;_execute($row, $rules, $val, $cycles); $cycles++; } return; } // -------------------------------------------------------------------- // If the field is blank, but NOT required, no further tests are necessary $callback = FALSE; if ( ! in_array('required', $rules) AND is_null($postdata)) { // Before we bail out, does the rule contain a callback? if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match)) { $callback = TRUE; $rules = (array('1' =&gt; $match[1])); } else { return; } } // -------------------------------------------------------------------- // Isset Test. Typically this rule will only apply to checkboxes. if (is_null($postdata) AND $callback == FALSE) { if (in_array('isset', $rules, TRUE) OR in_array('required', $rules)) { // Set the message type $type = (in_array('required', $rules)) ? 'required' : 'isset'; if(array_key_exists($row['field'],$this-&gt;_custom_messages) &amp;&amp; array_key_exists($type,$this-&gt;_custom_messages[$row['field']])){ $line = $this-&gt;_custom_messages[$row['field']][$type]; }else{ if ( ! isset($this-&gt;_error_messages[$type])) { if (FALSE === ($line = $this-&gt;CI-&gt;lang-&gt;line($type))) { $line = 'The field was not set'; } } else { $line = $this-&gt;_error_messages[$type]; } } // Build the error message $message = sprintf($line, $this-&gt;_translate_fieldname($row['label'])); // Save the error message $this-&gt;_field_data[$row['field']]['error'] = $message; if ( ! isset($this-&gt;_error_array[$row['field']])) { $this-&gt;_error_array[$row['field']] = $message; } } return; } // -------------------------------------------------------------------- // Cycle through each rule and run it foreach ($rules As $rule) { $_in_array = FALSE; // We set the $postdata variable with the current data in our master array so that // each cycle of the loop is dealing with the processed data from the last cycle if ($row['is_array'] == TRUE AND is_array($this-&gt;_field_data[$row['field']]['postdata'])) { // We shouldn't need this safety, but just in case there isn't an array index // associated with this cycle we'll bail out if ( ! isset($this-&gt;_field_data[$row['field']]['postdata'][$cycles])) { continue; } $postdata = $this-&gt;_field_data[$row['field']]['postdata'][$cycles]; $_in_array = TRUE; } else { $postdata = $this-&gt;_field_data[$row['field']]['postdata']; } // -------------------------------------------------------------------- // Is the rule a callback? $callback = FALSE; if (substr($rule, 0, 9) == 'callback_') { $rule = substr($rule, 9); $callback = TRUE; } // Strip the parameter (if exists) from the rule // Rules can contain a parameter: max_length[5] $param = FALSE; if (preg_match("/(.*?)\[(.*)\]/", $rule, $match)) { $rule = $match[1]; $param = $match[2]; } // Call the function that corresponds to the rule if ($callback === TRUE) { if ( ! method_exists($this-&gt;CI, $rule)) { continue; } // Run the function and grab the result $result = $this-&gt;CI-&gt;$rule($postdata, $param); // Re-assign the result to the master data array if ($_in_array == TRUE) { $this-&gt;_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result; } else { $this-&gt;_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result; } // If the field isn't required and we just processed a callback we'll move on... if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE) { continue; } } else { if ( ! method_exists($this, $rule)) { // If our own wrapper function doesn't exist we see if a native PHP function does. // Users can use any native PHP function call that has one param. if (function_exists($rule)) { $result = $rule($postdata); if ($_in_array == TRUE) { $this-&gt;_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result; } else { $this-&gt;_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result; } } else { log_message('debug', "Unable to find validation rule: ".$rule); } continue; } $result = $this-&gt;$rule($postdata, $param); if ($_in_array == TRUE) { $this-&gt;_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result; } else { $this-&gt;_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result; } } // Did the rule test negatively? If so, grab the error. if ($result === FALSE) { if(array_key_exists($row['field'],$this-&gt;_custom_messages) &amp;&amp; array_key_exists($rule,$this-&gt;_custom_messages[$row['field']])){ $line = $this-&gt;_custom_messages[$row['field']][$rule]; }else{ if ( ! isset($this-&gt;_error_messages[$rule])) { if (FALSE === ($line = $this-&gt;CI-&gt;lang-&gt;line($rule))) { $line = 'Unable to access an error message corresponding to your field name.'; } } else { $line = $this-&gt;_error_messages[$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" if (isset($this-&gt;_field_data[$param]) AND isset($this-&gt;_field_data[$param]['label'])) { $param = $this-&gt;_translate_fieldname($this-&gt;_field_data[$param]['label']); } // Build the error message $message = sprintf($line, $this-&gt;_translate_fieldname($row['label']), $param); // Save the error message $this-&gt;_field_data[$row['field']]['error'] = $message; if ( ! isset($this-&gt;_error_array[$row['field']])) { $this-&gt;_error_array[$row['field']] = $message; } return; } } } // END MY Form Validation Class /* End of file MY_Form_validation.php */ /* Location: ./application/libraries/MY_Form_validation.php */ } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload