Codeigniter Form Validation - Is there a way to define multiple different %s when setting error messages?

淺唱寂寞╮ 提交于 2019-12-24 15:17:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!