繁体   English   中英

Codeigniter 3中的表单验证问题

[英]Issue with form validation in codeigniter 3

我正在使用最新版本的Codeigniter,但是有问题。 每当我提交具有必要详细信息的数据时,都不会插入数据库, form_validation->run() == FALSE总是返回false,就像所提交的数据存在问题一样。

这是我的代码:

class Auth extends CI_Controller {

public function __construct()
{
    // Call the Model constructor
    parent::__construct();
    $this->load->database();
    $this->load->model('user_model'); //database inserter

}
public function submit_join(){
    $this->form_validation->set_rules('username', 'Username', 'is_unique[students.username]');
    $this->form_validation->set_rules('email', 'Email', 'valid_email|is_unique[students.email]');
    $this->form_validation->set_rules('phone', 'Mobile No.', 'numeric');
    $this->form_validation->set_rules('password', 'Password', 'min_length[4]|max_length[20]|matches[confirm_pass]|md5');
    $this->form_validation->set_rules('confirm_pass', 'Confirm Password', 'matches[password]|md5');

    if ($this->form_validation->run() == FALSE)
    {
        // fails
       $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There was an error when verifying your details!</div>');
        $this->load->view('join');
    }

     else
    {
        //insert the user registration details into database
        $data = array(
            'full_name' => $this->input->post('full_name'),
            'username' => $this->input->post('username'),
            'email' => $this->input->post('email'),
            'password' => $this->input->post('password'),
            'address' => $this->input->post('address'),
            'sex' => $this->input->post('sex'),
            'state' => $this->input->post('state'),
            'phone' => $this->input->post('phone')
        );

     //Insert to database function
   }

}

您是否加载了表单验证库? 它不在您的构造函数或函数中,因此可以解决此问题。

public function __construct()
{
    // Call the Model constructor
    parent::__construct();
    $this->load->database();
    $this->load->model('user_model'); //database inserter

    //Add this library
    $this->load->library('form_validation');
}

首先,检查所有字段名称是否与您在验证规则中设置的名称相同,然后检查is_unique[students.email]在所连接的数据库中是否为有效字段,如果一切正常,则您的学生数据库包含电子邮件字段,然后尝试在控制器中正确加载表单验证库,

在确认密码规则中删除md5。 将表单验证错误设置为视图中的表单,例如

<?php echo form_error('name');?>

发现错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM