我正在尝试使用不同的错误消息设置codeigniter表单.
set_message(rule,msg)正在为整个表单设置一条消息.
我需要:
$this->form_validation->set_rules('name', 'First Name', 'required');
$this->form_validation->set_message('name', 'required', 'Enter your Name');
$this->form_validation->set_rules('second', 'Variables', 'required');
$this->form_validation->set_message('second', 'required',
'The Variables are required');
Run Code Online (Sandbox Code Playgroud)
在这种情况下,将%s添加到消息字符串中没有帮助,因为消息必须完全不同.
可能我可以这样做:
$this->form_validation->set_rules('name', 'Name',
'required|min_length[6]|max_length[12]');
$this->form_validation->set_rules('second', 'Variables',
'required|min_length[3]|max_length[5]');
$this->form_validation->set_message('required', 'required');
$this->form_validation->set_message('min_length', 'short');
$this->form_validation->set_message('max_length', 'long');
Run Code Online (Sandbox Code Playgroud)
switch(form_error('name')) {
case '<p>required</p>':
echo 'Enter your Name';
break;
case '<p>short</p>':
echo 'min length 6';
break;
case '<p>long</p>':
echo 'min length 12';
break;
}
switch(form_error('second')) {
case '<p>required</p>':
echo 'The Variables are required';
break;
case '<p>short</p>':
echo 'min length 3';
break;
case '<p>long</p>':
echo 'min length 5';
break;
}
Run Code Online (Sandbox Code Playgroud)
但是,有没有一种更聪明的方法呢?
小智 8
我认为更聪明的方法是使用Codeigniter的回调功能(类似于下面的内容).以下工作,但可能更精简它.如果不出意外,这是一个起点.
创建两个回调函数(我将这些函数命名为custom_required和custom_check_length)并将它们放在控制器的底部(或者您认为必要的位置).
private function _custom_required($str, $func) {
switch($func) {
case 'name':
$this->form_validation->set_message('custom_required', 'Enter your name');
return (trim($str) == '') ? FALSE : TRUE;
break;
case 'second':
$this->form_validation->set_message('custom_required', 'The variables are required');
return (trim($str) == '') ? FALSE : TRUE;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
和...
private function _custom_check_length($str, $params) {
$val = explode(',', $params);
$min = $val[0];
$max = $val[1];
if(strlen($str) <= $max && strlen($str) >= $min) {
return TRUE;
} elseif(strlen($str) < $min) {
$this->form_validation->set_message('custom_check_length', 'Min length ' . $min);
return FALSE;
} elseif(strlen($str) > $max) {
$this->form_validation->set_message('custom_check_length', 'Max length ' . $max);
return FALSE;
}
}
Run Code Online (Sandbox Code Playgroud)
这两个函数负责表单验证的set_message方面.要设置规则,您只需要通过在函数名前加上callback_来调用这两个函数.
所以...
$this->form_validation->set_rules('name', 'Name', 'callback__custom_required[name]|callback__custom_check_length[6,12]');
$this->form_validation->set_rules('second', 'Second', 'callback__custom_required[second]|callback__custom_check_length[3,5]');
Run Code Online (Sandbox Code Playgroud)
我希望上面有所帮助!!
小智 5
您可以像我下面提到的那样设置自定义错误,无需为此错误消息内容更改创建自定义函数.
$validation = array(
array(
'field' => 'name',
'label' => 'NAME',
'rules' => 'trim|required',
"errors" => array('required' => " Enter your %s. ")
),
);
$this->form_validation->set_rules($validation);
if ($this->form_validation->run()) {}
Run Code Online (Sandbox Code Playgroud)