表单验证在codeigniter中无法正常工作

时间:2015-12-22 10:05:46

标签: php codeigniter

表格验证在codeigniter中无法正常工作。我写了下面给出的代码。当我点击提交按钮时,表单会自动提交,没有任何值。 (即,不进行验证)

代码

     <html>
    <head>
    </head>
    <body>
    <?php
    echo form_open_multipart("form_validate/validate");
        echo form_fieldset("Registration Form");
        echo "Name :".nbs(5).form_input('name','').form_error("name","<span style='color:red;'>","</span>").br(1);
        echo "Username :".nbs(5).form_input('username','').form_error("username","<span style='color:red;'>","</span>").br(1);
        echo "Email :".nbs(5).form_input('email','').form_error("email","<span style='color:red;'>","</span>").br(1);
        echo "password :".nbs(5).form_input('password','').form_error("password","<span style='color:red;'>","</span>").br(1);
        echo "Confirm-Password :".nbs(5).form_input('cpassword','').form_error("cpassword","<span style='color:red;'>","</span>").br(1);
        echo "Mobile :".nbs(5).form_input('mobile','').form_error("mobile","<span style='color:red;'>","</span>").br(1);    
        echo form_submit('submit','Register').nbs(5).form_reset('reset','Cancel').br(1);    
        echo form_fieldset_close();
        echo form_close();
    ?>
    </body>
 </html>

控制器代码

<?php

class User extends CI_Controller
{
    public function main()
    {   
        $this -> load -> helper('html');
        $this -> load -> helper('form');
        $this -> load -> view('form');      
    }

    public function validate(){
            $this   ->  load    ->  library("form_validation");
            $this   ->  load    ->  helper("html");
            $this   ->  load    ->  helper("form");
            $this   ->  form_validation ->  set_rules('name','NAME','required');
            $this   ->  form_validation ->  set_rules('username','USERNAME','required|min_length[3]|max_length[10]|alpha');
            $this   ->  form_validation ->  set_rules('email','Email','required|valid_emails');         
            $this   ->  form_validation ->  set_rules('password','Password','required|matches[cpassword]'); 
            $this   ->  form_validation ->  set_rules('cpassword','Confirm-Password','required');
            $this   ->  form_validation ->  set_rules('mobile','Mobile','required|exact_length[10]|integer');;

        if($this -> form_validation ->run()==false)
                {
                $this -> load -> view('form');
                }
                else
                {
                echo "form has been Submitted";
                }
    }
}

?>

我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

在控制器中试试这个

class User extends CI_Controller
{
 public function __construct(){
    $this->load->helper('html');
    $this->load->helper('form');
    $this->load->library("form_validation");
 }
 public function main()
 {          
    $this -> load -> view('form');      
 }
 public function save(){       
        $this   ->  form_validation ->  set_rules('name','NAME','required');
        $this   ->  form_validation ->  set_rules('username','USERNAME','required|min_length[3]|max_length[10]|alpha');
        $this   ->  form_validation ->  set_rules('email','Email','required|valid_emails');         
        $this   ->  form_validation ->  set_rules('password','Password','required|matches[cpassword]'); 
        $this   ->  form_validation ->  set_rules('cpassword','Confirm-Password','required');
        $this   ->  form_validation ->  set_rules('mobile','Mobile','required|exact_length[10]|integer');;

    if($this -> form_validation ->run()==false)
    {
       $this -> load -> view('form');
    }
    else
    {
       echo "form has been Submitted";
    }
  }
}

并在视图中更改此

echo form_open_multipart("form_validate/validate");

echo form_open_multipart("form_validate/save");