Codeigniter 2的自定义表单验证错误消息 [英] Custom form validation error message for Codeigniter 2
问题描述
我有一个名为business_id的下拉菜单。
I have a drop down named "business_id".
<select name="business_id">
<option value="0">Select Business</option> More options...
</select>
这里有验证规则,用户必须选择一个选项。
Here comes the validation rule, user must select an option.
$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]');
问题是错误消息说:业务字段必须包含大于0的数字。不是很直观!
Problem being the error message says: The Business field must contain a number greater than 0. Not very intuitive! I want it to say "You must select a business".
我试过:
$this->form_validation->set_message('Business', 'You must select a business');
但CI完成忽略此。
推荐答案
尝试不对默认选择...设置value属性...
Try not setting the value attribute on the default select...
<select name="business_id">
<option value>Select Business</option> More options...
</select>
,然后只需使用您的表单验证规则...
and then just using required for your form validation rule...
$this->form_validation->set_rules('business_id', 'Business', 'required');
我想你可以尝试编辑你尝试设置消息的方式...
I suppose you could try editing the way that you're trying to set the message also...
$this->form_validation->set_message('business_id', 'You must select a business');
instead of
$this->form_validation->set_message('Business', 'You must select a business');
我不完全确定是否会做这个伎俩。
I'm not entirely sure if that will do the trick though.
这篇关于Codeigniter 2的自定义表单验证错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!