如何在codeigniter中验证表单

时间:2014-06-09 22:36:34

标签: php forms codeigniter views

我正在使用代码点火器2.1.0广告我试图验证表格(虽然这个表格是在这个网站的主页上找到的),已经遵循了这个文档,但似乎没有按预期工作。每次我包括页面重新排列的库。请有人告诉我可能有什么不对。我的页面部分是我想要的。

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

3 个答案:

答案 0 :(得分:0)

首先,您必须包含来自控制器的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
}

答案 1 :(得分:0)

我建议您使用表单助手而不是HTML标记,它可以让您的网站在您的网址发生变化时更具可移植性。所以你的观点看起来会这样:

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

答案 2 :(得分:0)

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

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

我也使用了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');
    }