php 无法访问与您的字段名称对应的错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10613010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to access an error message corresponding to your field name
提问by David Yang Liu
I have a callback function that check_captcha which sees if $rowis ==0or == 1(this information is queried from sql).
我有一个回调函数 check_captcha 它查看是否$row是==0或== 1(此信息是从 sql 查询的)。
The problem is that I can not call it from $self->form_validation->set_rule('captcha', 'call_back_check_captcha')due to the fact that my function takes in a $rowvar. The way I'm calling it now I get a Unable to access error message. How can I make this work?
问题是我无法调用它,$self->form_validation->set_rule('captcha', 'call_back_check_captcha')因为我的函数接受了一个$rowvar。我现在调用它的方式是收到无法访问错误消息。我怎样才能使这项工作?
function check_captcha( $row)
{
if($row ==0)//didnt find any
{
$this->form_validation->set_message('captcha', 'text dont match captcha');
return FALSE;
}
else
{
return TRUE;
}
}
function create_member()
{
$past = time() - 7200;
$this->db->query("DELETE FROM captcha WHERE captcha_time <".$past);
$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word =? AND ip_address =?";
$binds = array($_POST['captcha'], $this->input->ip_address(), $past);
$query= $this->db->query($sql, $binds);
$row = $query->row(); //row query rows : if it found an entry =1
$self->check_captcha($row->count);
//VALIDATIONS
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules( 'email_address', 'Email Address', 'trim|required|valid_email|unique[user.email_address]');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|unique[user.username]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_leng[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation','trim|required|matches[password]');
if(!$_POST['captcha']){
$this->form_validation->set_rules('captcha', 'Captcha','trim|required');}else{
$this->form_validation->set_rules('captcha', 'Captcha', 'callback_check_captcha');}
if($this->form_validation->run()==FALSE)
{ //this -> to the curr obj(UserController) && registraion() points to the the function in controller
$this->registration(); //reloads reg page so they can fill out right stuff
}
else
回答by Laurence
$this->form_validation->set_message('check_captcha', 'text dont match captcha');
The message name corresponds to the function, not the field. So setting it to "check_captcha" will fix your bug. The error message will use the correct field name.
消息名称对应于函数,而不是字段。因此,将其设置为“ check_captcha”将修复您的错误。错误消息将使用正确的字段名称。
回答by Helton Ritter
Actually the best way, instead of write the error message directly on controller, would be add this entry "check_captcha" on languages.
实际上,最好的方法是在语言上添加此条目“check_captcha”,而不是直接在控制器上写入错误消息。
In my case, the message for validation rule (form validation) "less_than" was not present.
就我而言,验证规则(表单验证)“less_than”的消息不存在。
I changed the file /system/language/??/form_validation_lang.php. I've added the missing entry.
我更改了文件/system/language/??/form_validation_lang.php。我已经添加了缺失的条目。
回答by Muhammad Tahir
That helped me
那帮助了我
go to application/config/autoload.php and add "Security" helper class there.
转到 application/config/autoload.php 并在那里添加“Security”助手类。
$autoload['helper'] = array('security');
Or add this before your form validation
或者在表单验证之前添加这个
$this->load->helper('security');
回答by StefB
add a entry to your language file with named of the part inside the (yourfieldname) of the errormessage - thats solved the problem
在您的语言文件中添加一个条目,并命名为错误消息(您的字段名)内的部分 - 这解决了问题
回答by Goshika Mahesh
You can set error messagein set_rules:
您可以在以下位置设置错误消息set_rules:
$this->form_validation->set_rules('captcha', 'Captcha', 'callback_check_captcha',
$this->form_validation->set_rules('captcha', 'Captcha', 'callback_check_captcha',
array('check_captcha' => 'text dont match captcha'));
array('check_captcha' => 'text dont match captcha'));

