我有一个名为"business_id"的下拉列表。
<select name="business_id">
<option value="0">Select Business</option> More options...
</select>
验证规则来了,用户必须选择一个选项。
$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]');
问题是错误消息说:Business字段必须包含一个大于0的数字。不是很直观!我想让它说"你必须选择一个行业"。
我试着:
$this->form_validation->set_message('Business', 'You must select a business');
但是CI完全忽略了这一点。有人能解决这个问题吗?
我对在codeigniter 2中添加自定义表单验证错误消息有相同的需求(例如:"你必须同意我们的条款;条件")。当然,覆盖require和greater_than的错误消息是错误的,因为它会错误地为表单的其余部分生成消息。我扩展了CI_Form_validation类,并覆盖了set_rules方法,以接受一个新的"message"参数:
<?php
class MY_Form_validation extends CI_Form_validation
{
private $_custom_field_errors = array();
public function _execute($row, $rules, $postdata = NULL, $cycles = 0)
{
// Execute the parent method from CI_Form_validation.
parent::_execute($row, $rules, $postdata, $cycles);
// Override any error messages for the current field.
if (isset($this->_error_array[$row['field']])
&& isset($this->_custom_field_errors[$row['field']]))
{
$message = str_replace(
'%s',
!empty($row['label']) ? $row['label'] : $row['field'],
$this->_custom_field_errors[$row['field']]);
$this->_error_array[$row['field']] = $message;
$this->_field_data[$row['field']]['error'] = $message;
}
}
public function set_rules($field, $label = '', $rules = '', $message = '')
{
$rules = parent::set_rules($field, $label, $rules);
if (!empty($message))
{
$this->_custom_field_errors[$field] = $message;
}
return $rules;
}
}
?>
使用上面的类,您将生成带有自定义错误消息的规则,如下所示:
$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]', 'You must select a business');
您也可以在自定义消息中使用'%s',它将自动填充fieldname的标签。
如果您想定制每个规则显示的错误消息,您可以在数组中找到它们:
/system/language/english/form_validation_lang.php
尽量不要将value属性设置为默认的select…
<select name="business_id">
<option value>Select Business</option> More options...
</select>
然后使用所需的表单验证规则…
$this->form_validation->set_rules('business_id', 'Business', 'required');
我想你可以试着编辑你想要设置的消息的方式…
$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');
我不完全确定这是否会奏效。
你应该像Anthony说的那样扩展Form_validation库。
例如,我在一个名为MY_Form_validation.php
的文件中这样做它应该放在/application/libraries
function has_selection($value, $params)
{
$CI =& get_instance();
$CI->form_validation->set_message('has_selection', 'The %s need to be selected.');
if ($value == -1) {
return false;
} else {
return true;
}
}
在您的情况下,因为您的第一个选项(指导选项-请选择…)的值为0
,您可能希望将条件语句从-1
更改为0
。然后,从现在开始,您可以使用这一行来检查选择值:
$this->form_validation->set_rules('business_id', 'Business', 'has_selection');
希望这对你有帮助!
这是我使用的一个简单的CI2回调函数。我想要的东西不仅仅是"required"作为验证的默认参数。文档帮助:http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks
public function My_form() {
...Standard CI validation stuff...
$this->form_validation->set_rules('business_id', 'Business', 'callback_busid');
...
if ($this->form_validation->run() == FALSE) {
return false;
}
else {
...process the form...
$this->email->send();
}
} // Close My_form method
// Callback method
function busid($str) {
if ($str == '') {
$this->form_validation->set_message('business_id', 'Choose a business, Mang!');
return FALSE;
}
else {
return TRUE;
}
} // Close the callback method
对于您的情况,您可以更改回调以检查if($str<0)
-我假设您在选择/下拉菜单中使用了数字。
如果回调返回false,则保留表单并显示错误消息。否则,它将被传递并发送给form method
的'else'。
一个小hack可能对你不好,但是我已经做了一个小改变。
,我想更改消息' Email字段必须是唯一值'。
我已经这样做了
<?php
$error = form_error('email');
echo str_replace('field must be a unique value', 'is already in use.', $error);
// str_replace('string to search/compare', 'string to replace with', 'string to search in')
?>
如果找到字符串,则打印我们的自定义消息,否则它将显示错误消息,如' Email字段必须是有效的电子邮件'等…
对于正在使用CodeIgniter 3的人,您可以执行以下操作:
$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]', array(
'greater_than' => 'You must select a business',
));
如果您正在使用CodeIgniter 2,您将需要用新的CodeIgniter 3 CI_Form_validation类扩展和覆盖CI_Form_validation类(https://ellislab.com/codeigniter/user-guide/general/creating_libraries.html获取更多关于如何这样做的信息),并使用上面的函数
规则名是最后一个参数。
请尝试:
$this->form_validation->set_message('greater_than[0]', 'You must select a business');
更多信息:https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html validationrules
我用一个简单的函数扩展了form_validation库,该函数确保下拉框没有选择其默认值。希望这对你有所帮助。
应用程序/图书馆/MY_Form_validation.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
class MY_Form_validation extends CI_Form_validation {
function __construct()
{
parent::__construct();
$this->CI->lang->load('MY_form_validation');
}
/**
* Make sure a drop down field doesn't have its default value selected.
*
* @access public
* @param string
* @param field
* @return bool
* @author zechdc
*/
function require_dropdown($str, $string_to_compare)
{
return ($str == $string_to_compare) ? FALSE : TRUE;
}
}
应用程序/语言/英语/MY_Form_validation_lang.php
$lang['require_dropdown'] = 'The %s field must have an item selected.';
如何使用:
1)创建下拉框:<select name="business_id">
<option value="select">Select Business</option> More options...
</select>
2)创建验证规则。你可能可以将值设置为0并使用require_dropdown[0],但我从未尝试过。
$this->form_validation->set_rules('business_id', 'Business', 'require_dropdown[select]');
3)设置您的自定义消息:(或跳过此步骤,使用语言文件中的消息)
$this->form_validation->set_message('business_id', 'You must select a business');
创建方法username_check回调函数
01。
public function username_check($str)
{
if ($str=="")
{
$this->form_validation->set_message('username_check', 'Merci d’indiquer le nombre d’adultes');
return FALSE;
}
else
{
return TRUE;
}
}
——02. 然后把这个验证码放到你的类
上$this->form_validation->set_rules('number_adults', 'Label Name','Your Message',) 'callback_username_check');