根据documentation,在设置错误消息时,%s将被字段名替换。
如果在错误字符串中包含%s,则会将其替换为 你设定规则时用于你的领域的“人”名称。
是否有办法在字符串中包含多个%s并以不同方式定义它们?
答案 0 :(得分:0)
This是表单验证库中用于创建错误消息的行:
// Build the error message
$message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
$line
:错误消息模板,例如“%字段无效”$this->_translate_fieldname($row['label'])
:字段标签$param
:参数传递了验证函数,如果有的话,max_length[5]
会传递“5”这就是表单验证类为您所做的工作。如果您需要更大的灵活性,您必须预先prepare the error message yourself < - 可能有用,并且已经填充了额外的变量。
通过core/MY_form_validation.php
扩展表单验证类并使用此功能可能会很有趣。不幸的是,这一行包含在主函数_execute
中,它非常大而且非常混乱,并且从查看现在,你还需要重载其他一些功能。我开始写一个例子,但它实际上看起来像一件苦差事。你可能最好使用:
$this->form_validation->set_message('rule_name', 'Your custom message here');
有关设置错误消息的更多信息:http://ellislab.com/codeigniter/user_guide/libraries/form_validation.html#settingerrors