Codeigniter表单验证不起作用?

时间:2017-10-18 04:30:27

标签: forms codeigniter validation

我创建了注册表单,包括以下字段,如

  • 名称
  • 电子邮件
  • 作用
  • 密码
  • 确认密码

但表单验证无效

我尝试了很多但不起作用是我想在Config.php中更改任何内容我已经更改了$ config [library],$ config [helper]可以帮助我在这段代码中。提前谢谢

这是我的控制器

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Mod_saloon');
        $this->load->helper(array('form', 'url'));
        $this->load->library(array('session', 'form_validation', 'email')); 
        $this->load->helper('url');
    }
    public function index()
    {
        $this->load->view('login');
    }
    public function show_signup()
    {
        $this->load->view('signup');
    }
    public function signup()
    {
        $config = array(
        array(
                'field' => 'name',
                'label' => 'Username',
                'rules' => 'required|min_length[5]|max_length[12]|is_unique[signup.name]'
        ),
        array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required',
                'errors' => array(
                        'required' => 'You must provide a %s.',
                ),
        ),
        array(
                'field' => 'cpassword',
                'label' => 'Password Confirmation',
                'rules' => 'required|matches[password]'
        ),
        array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required|valid_email|is_unique[signup.email]'
        )
);

$this->form_validation->set_rules($config);

        $this->form_validation->set_rules(
        'name', 'Username',
        'required|min_length[5]|max_length[12]|is_unique[signup.name]',
        array(
            'required'      => 'You have not provided %s.',
            'is_unique'     => 'This %s already exists.'
        )
        );
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('cpassword', 'Password Confirmation', 'required|matches[password]');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[signup.email]');
        if ($this->form_validation->run() == FALSE) 
        {
            echo "<script>alert('Validation Error');</script>";
            $this->load->view('signup');
        } 
        else 
        {
            $name = NULL;
            $role=NULL;
            $email=NULL;
            $password=NULL;
            $cpassword=NULL;

            extract($_POST);

            $signup['$name'] = $name;
            $signup['$role'] = $role;
            $signup['$email'] = $email;
            $signup['$password'] = $password;
            $signup['$cpassword'] = $cpassword;

            $this->Mod_saloon->new_signup($signup);

            $this->load->view('signup');
        }


    }
}

这是我的视图

<form action="signup" method="post">
            <div class="form-group has-feedback">
                <input type="text" class="form-control" name="name" placeholder="Name">
                <span class="glyphicon glyphicon-user form-control-feedback"></span>
            </div>
            <select class="form-control" name="role">
                <option disabled selected value> --Select the Role-- </option>
                <option value="admin">Admin</option>
                <option value="user">User</option>
            </select>
            <div class="form-group has-feedback">
                <input type="email" class="form-control" name="email" placeholder="Email">
                <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
            </div>
            <div class="form-group has-feedback">
                <input type="password" class="form-control" name="password" placeholder="Password">
                <span class="glyphicon glyphicon-lock form-control-feedback"></span>
            </div>
            <div class="form-group has-feedback">
                <input type="password" class="form-control" name="cpassword" placeholder="Confirm Password">
                <span class="glyphicon glyphicon-lock form-control-feedback"></span>
            </div>
            <div class="gap"></div>
            <button type="submit" class="btn btn-primary btn-block btn-flat centered" style="width:35%;">Create User</button>
        </form>

3 个答案:

答案 0 :(得分:0)

我认为你的问题并不是你的表单验证不起作用...在你的控制器中你的“注册”功能似乎没有在任何地方运行。你需要检查你的路由和用于运行的函数您需要致电的链接

  

$这 - &GT;注册

您还需要在表单提交时调用它,因此在您的视图中,提交按钮必须具有名称和值(例如:name =“create_user”,value =“something”)注册您应该具有

  

if($ this-&gt; input-&gt; post('create_user')){做表单验证的事情}

答案 1 :(得分:0)

之类的形式更改操作
<form action="<?php echo base_url()?>index.php/Login/signup" method="post">

并且无需多次设置规则。要么你可以使用数组方法,要么单独设置规则

答案 2 :(得分:0)

您的代码需要大量修正

  1. 表格标签
  2. 您需要先更改表单标记:

    <form action="<?= base_url('login/signup') ?>" method="post">
    
    1. 将您的Config数组放入config文件夹中。 在form_validation.php中添加您的配置数组,在此文件中添加以下代码。
    2.   

      应用/配置/ form_validation.php

      $config = array(
          'signup_validation' => array(
              array(
                      'field' => 'name',
                      'label' => 'Username',
                      'rules' => 'required|min_length[5]|max_length[12]|is_unique[signup.name]'
              ),
              array(
                      'field' => 'password',
                      'label' => 'Password',
                      'rules' => 'required',
                      'errors' => array(
                              'required' => 'You must provide a %s.',
                      ),
              ),
              array(
                      'field' => 'cpassword',
                      'label' => 'Password Confirmation',
                      'rules' => 'required|matches[password]'
              ),
              array(
                      'field' => 'email',
                      'label' => 'Email',
                      'rules' => 'required|valid_email|is_unique[signup.email]'
              )
          )
          );
      
      1. 您的控制器功能
      2. 以下是控制器功能的代码

        public function signup()
        {
        
            if ($this->form_validation->run('signup_validation') == TRUE) {
        
                // Your sign up the code if validation is true. 
        
            } else {
                // load the view 
        
        
                //User validation_errors(); to show all your validation error at view
            }
        }