2014-02-27 59 views
1

在這裏工作是控制器表單驗證CI並不適用於所有領域

class Form extends CI_Controller { 

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

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

    $config = array(
      array(
       'field' => 'username', 
       'label' => 'Username', 
       'rules' => 'trim|required|min_length[5]|max_length[12]|is_unique[users.username]|xss_clean' 
      ), 
      array(
       'field' => 'password', 
       'label' => 'Password', 
       'rules' => 'trim|required|matches[passconf]|md5' 
      ), 
      array(
       'field' => 'passconf', 
       'label' => 'Password Confirmation', 
       'rules' => 'trim|required' 
      ), 
      array(
       'field' => 'email', 
       'label' => 'Email', 
       'rules' => 'trim|required|valid_email|is_unique[users.email]' 
      ) 
     ); 

    $this->form_validation->set_rules($config); 

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

,這裏是意見(myform.php,formsuccess.php)

myform.php

<html> 
<head> 
<title>My Form</title> 
</head> 
<body> 
<?php echo validation_errors(); ?> 
<?php echo form_open('form'); ?> 
<h5>Username</h5> 
<input type="text" name="username" value="" size="50" /> 
<h5>Password</h5> 
<input type="text" name="password" value="" size="50" /> 
<h5>Password Confirm</h5> 
<input type="text" name="passconf" value="" size="50" /> 
<h5>Email Address</h5> 
<input type="text" name="email" value="" size="50" /> 
<div><input type="submit" value="Submit" /></div> 
</form> 
</body> 
</html> 

formsuccess.php

<html> 
<head> 
<title>My Form</title> 
</head> 
<body> 
<h3>Your form was successfully submitted!</h3> 
<p><?php echo anchor('form', 'Try it again!'); ?></p> 
</body> 
</html> 

在我運行它之後,如果用戶名不符合要求,驗證會起作用,但是當用戶名滿足要求時,驗證不起作用並進入成功頁面,它應該適用於窗體上的所有字段,而不是隻是用戶名。我該怎麼辦?對不起,我的英語不好

+0

你試過'$ this-> form_validation-> run()=== FALSE'? –

+0

不工作夥伴:(任何其他建議? –

+0

嘗試刪除is_unique [users.username]和is_unique [users.email],並查看它是否有效 – Andrew

回答

0

formigniter.org/app

我更喜歡使用這個,它會自動添加驗證。