我在使用Code Igniter时遇到了麻烦。我试图启用显示没有用的错误,据我所知,我正确地关注文档。我遇到的问题是模板中的validation_errors()函数不会回应验证问题。验证过程正在运行(如果验证失败,则返回到表单),但不显示错误消息。 然而,我已经从下面的控制器中包含了该方法并查看了表单。
$ this-> load-> model(' admin_members',' members',TRUE); if($ this-> input-> post(' addmember')){
$this->load->helper('form');
$this->load->library('form_validation');
// Displaying Errors In Div
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
// Validation For Name Field
$this->form_validation->set_rules('mname', 'Member Name', 'required|min_length[15]|max_length[25]');
$this->load->library('upload', $config);
// Validation For Email Field
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if($this->validation->run()== FALSE){
$this->load->view('admin/member-management/add', $data);
}
else{
$member_id = $this->members->add();
if(!is_dir('images/members/' . $member_id )) mkdir('images/members/' . $member_id , 0777, TRUE);
$config['upload_path'] = 'images/members/' . $member_id ."/";
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '22222';
$config['max_width'] = '10242';
$config['max_height'] = '10242';
$config['file_name'] = "profile.jpg" ;
$this->upload->initialize($config);
$this->upload->do_upload('profile_image');
$config = array();
$config['upload_path'] = 'images/members/' . $member_id ."/";
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '22222';
$config['max_width'] = '10242';
$config['max_height'] = '10242';
$config['file_name'] = "pan_card.jpg" ;
$this->upload->initialize($config);
$this->upload->do_upload('pan_card');
$config = array();
$config['upload_path'] = 'images/members/' . $member_id ."/";
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '22222';
$config['max_width'] = '10242';
$config['max_height'] = '10242';
$config['file_name'] = "id_proof.jpg" ;
$this->upload->initialize($config);
$this->upload->do_upload('id_proof');
$config = array();
$config['upload_path'] = 'images/members/' . $member_id ."/";
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '22222';
$config['max_width'] = '10242';
$config['max_height'] = '10242';
$config['file_name'] = "add_proof.jpg" ;
$this->upload->initialize($config);
$this->upload->do_upload('add_proof');
redirect('admin_member', 'refresh');
}
}
查看/ admin_member / add.php
答案 0 :(得分:0)
替换
$this->validation->run() == FALSE
要
$this->form_validation->run() === FALSE
请更正您在控制器功能中检查表单验证规则的语法。
答案 1 :(得分:0)
请务必在表单必须返回的视图中回显验证错误<?php echo validation_errors()?>
。
在您的代码中,您还传递了我注意到的变量$data
未在代码中的任何其他位置声明。我假设$data
是一个数组,它将为视图保存一些变量,或者假设$data
具有您传递给服务器的值。在这种情况下,您可以使用<?php echo set_values(//Value of the name parameter you give to the input)
。如果您想要返回表单但又不想再次填写表格,这可能很有用。
作为额外奖励使用$this-form_validation->run() === FALSE
代替$this->form_validation->run() == FALSE
。这不会解决问题,但它确保如果攻击者将元素的返回类型更改为字符串,则字符串“TRUE”将不会计算为TRUE
。
Lemme知道我是否回答了你的问题... :)