i'm trying to setup a codeigniter form with different error messages.
set_message(rule, msg) is setting up a message for the whole form.
I need:
$this->form_validation->set_rules('name', 'First Name', 'required');
$this->form_validation->set_message('name', 'required', 'Enter your Name');
$this->form_validation->set_rules('second', 'Variables', 'required');
$this->form_validation->set_message('second', 'required',
'The Variables are required');
Adding the %s into the message string is no help in this case, since the messages have to be completely different.
Possibly I could do something like:
$this->form_validation->set_rules('name', 'Name',
'required|min_length[6]|max_length[12]');
$this->form_validation->set_rules('second', 'Variables',
'required|min_length[3]|max_length[5]');
$this->form_validation->set_message('required', 'required');
$this->form_validation->set_message('min_length', 'short');
$this->form_validation->set_message('max_length', 'long');
switch(form_error('name')) {
case '<p>required</p>':
echo 'Enter your Name';
break;
case '<p>short</p>':
echo 'min length 6';
break;
case '<p>long</p>':
echo 'min length 12';
break;
}
switch(form_error('second')) {
case '<p>required</p>':
echo 'The Variables are required';
break;
case '<p>short</p>':
echo 'min length 3';
break;
case '<p>long</p>':
echo 'min length 5';
break;
}
But isn't there a smarter way to do it?
PHP_EOL; $data['err'] = $err; $this->load->view('viewname', $data); } else if ($this->form_validation->run() == true ) { #code... } else.. after setting your custom message to $err variable, print it on your view. Save this answer. Show activity on this post.
Setting Validation RulesCodeIgniter lets you set as many validation rules as you need for a given field, cascading them in order, and it even lets you prep and pre-process the field data at the same time. To set validation rules you will use the set_rules() method: $this->form_validation->set_rules();
Setting Validation Rules To set validation rules you will use the set_rules() function: $this->form_validation->set_rules(); The above function takes three parameters as input: The field name - the exact name you've given the form field.
I think a smarter way would be to use Codeigniter's callback feature (something similar to below). The following works but it may be possible to streamline it even more. If nothing else, it's a starting point.
Create two callback functions (I've named these custom_required and custom_check_length) and place them at the bottom of your controller (or wherever you feel necessary).
private function _custom_required($str, $func) {
switch($func) {
case 'name':
$this->form_validation->set_message('custom_required', 'Enter your name');
return (trim($str) == '') ? FALSE : TRUE;
break;
case 'second':
$this->form_validation->set_message('custom_required', 'The variables are required');
return (trim($str) == '') ? FALSE : TRUE;
break;
}
}
and...
private function _custom_check_length($str, $params) {
$val = explode(',', $params);
$min = $val[0];
$max = $val[1];
if(strlen($str) <= $max && strlen($str) >= $min) {
return TRUE;
} elseif(strlen($str) < $min) {
$this->form_validation->set_message('custom_check_length', 'Min length ' . $min);
return FALSE;
} elseif(strlen($str) > $max) {
$this->form_validation->set_message('custom_check_length', 'Max length ' . $max);
return FALSE;
}
}
These two functions take care of the set_message aspect of your form validation. To set the rules, you simply need to call these two functions by prefixing the function name with callback_.
So...
$this->form_validation->set_rules('name', 'Name', 'callback__custom_required[name]|callback__custom_check_length[6,12]');
$this->form_validation->set_rules('second', 'Second', 'callback__custom_required[second]|callback__custom_check_length[3,5]');
I hope the above helps in some way!!
you can set custom error like I mentioned below, no need to create custom function for this error message content change.
$validation = array(
array(
'field' => 'name',
'label' => 'NAME',
'rules' => 'trim|required',
"errors" => array('required' => " Enter your %s. ")
),
);
$this->form_validation->set_rules($validation);
if ($this->form_validation->run()) {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With