Codeigniter表单验证 - 是否有一种方法来定义多个不同的%s设置错误消息? [英] Codeigniter Form Validation - Is there a way to define multiple different %s when setting error messages?
问题描述
根据文档,%s将被字段名称替换
Based on the documentation, %s will be replaced by field name when setting error messages.
如果在错误字符串中包含%s,它将替换为
human
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.
有一种方法可以在字符串中包含多个%s,并以不同的方式定义它们?
Is there a way to have multiple %s in the string and define them differently?
推荐答案
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
:错误消息模板,例如%字段无效 -
$ this-> _translate_fieldname($ row ['label' code>:字段的标签
-
$ param
:参数传递验证函数$ c> max_length [5] 会传递5 $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.
通过 core / MY_form_validation.php
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');
有关设置错误消息的更多信息: http://ellislab.com/codeigniter/user_guide/libraries/form_validation.html#settingerrors
More on setting error messages: http://ellislab.com/codeigniter/user_guide/libraries/form_validation.html#settingerrors
这篇关于Codeigniter表单验证 - 是否有一种方法来定义多个不同的%s设置错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!