【问题标题】:Codeigniter validation is not workingCodeigniter 验证不起作用
【发布时间】:2014-04-08 11:49:00
【问题描述】:

我是 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');
}

【问题讨论】:

  • 有什么问题?
  • 请添加一些有关您的错误消息/错误日志的信息,以便我们为您提供帮助。
  • 您需要使用$this-&gt;form_validation-&gt;run()来调用测试。
  • 嗨 110precent ,我添加了这个 $this->form_validation->run() 。它工作正常。但我已经为字段设置验证规则 na ...它不工作。直接重定向到表单操作 ...

标签: php codeigniter validation


【解决方案1】:

在您的控制器中,当您调用“运行”方法时就是进程完成的时间:

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
  }
}

【讨论】:

  • 我只想检查这个函数 $this->form_validation->set_rules('username', 'Username', 'required');否则一切正常......
  • 如果不调用“run”方法,does 将无法按预期工作。调用“$this->form_validation->run();”之后应该是工作。试试看。
  • 我试过......在执行 $this->form_validation->run() 之前我已经编写了验证代码,对吗?所以第一次验证应该有效..对吗?
  • 感谢 wrivas !!实际上我犯了一个错误!我没有重定向到我写validation_error()的那个页面。你是对的。
【解决方案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

【讨论】:

  • 感谢提供帮助...我尝试了这些东西...只有我实现了相同的代码。
  • 没有显示错误。它正在重定向到我在控制器中编写的页面。在此处查看我的控制器 ...public function sm_login_action() { $this->load->helper(array('表格','网址')); $this->load->library('form_validation'); $this->form_validation->set_rules('username', 'Username', 'required'); if( $this->form_validation->run() ) { // 成功返回 TRUE $this->load->view('formsuccess'); } else { $this->load->view('formerror'); } }
【解决方案3】:

试试这个

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

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-24
    • 2012-07-14
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多