问题
Based on the documentation, %s will be replaced by field name when setting error messages.
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.
Is there a way to have multiple %s in the string and define them differently?
回答1:
This is the line in the form validation library that creates the error message:
// Build the error message
$message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
$line
: The error message template, for example "The % field is invalid"$this->_translate_fieldname($row['label'])
: The field's label$param
: The parameter passed the the validation function, if any,max_length[5]
would pass "5"
So that's the extent of what the form validation class does for you. If you need more flexibility you'll have to prepare the error message yourself <-- might be useful beforehand with the extra variables already populated.
It might be interesting to extend the form validation class via core/MY_form_validation.php
and work this functionality in. Unfortunately this line is contained in the main function _execute
which is very big and messy, and from looking at it now, you'd need to overload some other functions as well. I started to write an example but it actually looks like a chore. You might be better off using:
$this->form_validation->set_message('rule_name', 'Your custom message here');
More on setting error messages: http://ellislab.com/codeigniter/user_guide/libraries/form_validation.html#settingerrors
来源:https://stackoverflow.com/questions/13318914/codeigniter-form-validation-is-there-a-way-to-define-multiple-different-s-whe