【问题标题】:How to validate a form in codeigniter如何在codeigniter中验证表单
【发布时间】:2014-06-09 22:36:34
【问题描述】:

我正在使用 code igniter 2.1.0 ad 我试图验证一个表单(虽然这个表单是在这个网站的主页上找到的),已经遵循了这个文档,但似乎没有按预期工作。每次我包括页面重新排列的库。请有人告诉我可能出了什么问题。beow 是我试图验证的页面部分。

<?php echo validation_errors(); ?>
<form id="signup" action="" method="GET">
    <label>
        username:<input type="text" name="username" value="" size="31"/>
    </label>
    <br>
    <label>
        email:<input type="text" name="email" values="" size="31"/>
    </label>
    <br>
    <label>
        password:<input name="password" type="password" value="" size="31"/>
    </label>
    <br>
    <label>
        confirm password:<input name="passcon" type="password" value="" size="31"/>
    </label>
    <br>
    <label>
        date of birth:<input type="date" name="dob" value="" size="31" />
    </label>
    <br>
    <input type="radio" name="sex" value="male" /> Male<br />
    <input type="radio" name="sex" value="female" /> Female
    <br>

     <input id="button" type="submit" value="Signup" />
</form>

【问题讨论】:

标签: php forms codeigniter views


【解决方案1】:

首先你必须包含来自控制器的 form_validation 库:

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

其次你需要指定限制:

$this->form_validation->set_rules('username','Username','required|trim|minlength[6]');
$this->form_validation->set_rules('password','Password','required');
etc..

第三,您需要提交表单以防万一一切正常:

if($this->form_validation->run()){
   // All requirement are met
}else{
   // Not all requirement are met
}

【讨论】:

    【解决方案2】:

    我建议您使用表单助手而不是 HTML 标记,它可以让您的网站在您的 URL 发生变化时更加便携。所以在你看来它看起来像这样:

      <?php
      echo form_open('controllername/form_validation');
      echo validation_errors();
      $data = array(
              'size'=> '31',
            );
      echo form_input('username',$data);
      echo form_input('email',$data);
      echo form_password('password', $data);
      echo form_submit('login_submit', 'Login', $data);
      echo form_close();
    

    然后在你的控制器中,创建一个叫做表单验证的方法

    public function form_validation(){
     $this->load->library('form_validation');
     $this->form_validation->set_rules('username', 'Username', 'min_length[5]|required|');
     $this->form_validation->set_rules('email', 'Email', 'required|trim|xss_clean|valid_email');// This is where you configure your rules
     $this->form_validation->set_rules('password', 'Password', 'required|md5|trim');
        if ($this->form_validation->run())// If all the rules are respected, run() function return a boolean 
        {
         // Add the behavior to adopt
        }
    

    【讨论】:

      【解决方案3】:

      与 Amir 和 Vincent 一样,您应该加载库表单验证,我通常在 autoload.php 中加载库表单验证。

       $autoload['libraries'] = array('database', 'session','form_validation');
      

      我也将 session 用于 flashdata。好吧,也许你可以试试这个。

      查看:

      <?php echo $this->session->flashdata('flash_info'); ?>
      <form id="signup" action="" method="GET">
      <label>
          username:<input type="text" name="username" value="" size="31"/>
      </label>
      <br>
      <label>
          email:<input type="text" name="email" values="" size="31"/>
      </label>
      <br>
      <label>
          password:<input name="password" type="password" value="" size="31"/>
      </label>
      <br>
      <label>
          confirm password:<input name="passcon" type="password" value="" size="31"/>
      </label>
      <br>
      <label>
          date of birth:<input type="date" name="dob" value="" size="31" />
      </label>
      <br>
      <input type="radio" name="sex" value="male" /> Male<br />
      <input type="radio" name="sex" value="female" /> Female
      <br>
      
       <input id="button" type="submit" value="Signup" />
      </form>
      

      控制器:

      $this->form_validation->set_rules('username', 'Fullname', 'required|xss_clean'); 
      $this->form_validation->set_rules('email','Place Born', 'required|xss_clean');        $this->form_validation->set_rules('password','Fullname','required|xss_clean|matches[passcon]');
      
      $this->form_validation->set_rules('passcon','Confirm Password', 'required|xss_clean|');
      if($this->form_validate->run() == FALSE)
      {
          $this->session->set_flashdata('flash_info',validation_errors()); 
           redirect('your_controller');
      }else
      {
          $this->session->set_flashdata('flash_info',validation_errors()); 
          redirect('your_controller');
          }
      

      【讨论】:

        猜你喜欢
        • 2012-12-06
        • 2012-12-19
        • 1970-01-01
        • 1970-01-01
        • 2020-11-03
        • 2017-02-06
        • 2016-09-05
        相关资源
        最近更新 更多