Codeigniter验证无效

时间:2014-04-08 11:49:00

标签: php codeigniter validation

我是codeigniter的新手。我已经对我的项目进行了验证。但它工作不正常。我在这里编写了我的所有代码。首先是查看页面,第二是我的控制器页面。请帮助任何人

<?php $this->load->helper('form');  
  echo validation_errors();  
  echo form_open('SM_in_controller/sm_login_action');
?>

  <input class="login_input" type="text" placeholder="Username" name="username" id="username"/>
  <input type="submit" value="Login" class="login_button" id="login_button"/>
</form>  

和我的控制器

public function sm_login_action() {
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('username', 'Username', 'required');
}

3 个答案:

答案 0 :(得分:2)

在您的控制器中,当您调用“run”方法时,该过程就完成了:

public function sm_login_action() {
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('username', 'Username', 'required');

   if( $this->form_validation->run() ) { // Return TRUE on success
        // Success
   } else {
      // Failure
  }
}

答案 1 :(得分:2)

<?php

class Form extends CI_Controller {

    public function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }

    public function username_check($str)
    {
        if ($str == 'test')
        {
            $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

}
?>

试试这个以获取更多细节。 https://www.codeigniter.com/user_guide/libraries/form_validation.html

答案 2 :(得分:0)

试试这个

<input class="login_input" type="text" placeholder="Username" name="username" id="username" required/>

希望这有帮助