2013-02-17 117 views
3

我的表單驗證錯誤停止工作,它昨天工作,我一定做了錯誤的事情,但似乎無法找到它。CodeIgniter form_validation沒有顯示錯誤

當我填寫用戶名和電子郵件地址時,它會發送電子郵件和回顯的'The email has been sent!'。

當我只點擊註冊而沒有填寫任何信息,它只是重定向到加載視圖的用戶/註冊。

控制器/ user.php的:

public function signup_validation() { 
    $this -> load -> library('form_validation'); 

    $this -> form_validation -> set_rules('username', 'Username', 'trim|required|alpha|min_length[3]|max_length[25]'); 
    $this -> form_validation -> set_rules('email', 'Email', 'required|trim|valid_email|is_unique[users.email]'); 
    $this -> form_validation -> set_rules('password', 'Password', 'required|trim'); 
    $this -> form_validation -> set_rules('cpassword', 'Retype', 'required|trim|matches[password]'); 

    if ($this -> form_validation -> run()) { 
     $key = md5(uniqid()); 

     $this -> load -> library('email', array('mailtype' => 'html')); 
     $this -> load -> model('users'); 
     $this -> email -> from('', ""); 
     $this -> email -> to($this -> input -> post('email')); 
     $this -> email -> subject('Confirm your account.'); 
     $message = "<p>Thank you for signing up!</p>"; 
     $message = "<p><a href='" . base_url() . "user/activate/$key'>Click here</a> to confirm your account.</p>"; 
     $this -> email -> message($message); 
     if ($this -> users -> add_temp_user($key)) { 
      if ($this -> email -> send()) { 
       echo "The email has been sent!"; 
      } else { 
       echo "Could not send the email."; 
      } 
     } else { 
      echo "Problem adding user to database."; 
     } 
    } else { 
     redirect('user/signup'); 
    } 
} 

視圖/ signup.php

<form class="form-horizontal" action="<?php echo base_url() . 'user/signup_validation'; ?>" method="post" accept-charset="UTF-8"> 
    <?php echo validation_errors(); ?> 
    <div class="control-group"> 
     <label class="control-label" for="inputEmail">Username</label> 
     <div class="controls"> 
      <input name="username" type="text" id="inputUsername" placeholder="username" value="<?php echo $this -> input -> post('username'); ?>"/> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="inputEmail">Email</label> 
     <div class="controls"> 
      <input name="email" type="email" id="inputEmail" placeholder="email" value="<?php echo $this -> input -> post('email'); ?>"/> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="inputPassword">Password</label> 
     <div class="controls"> 
      <input name="password" type="password" id="inputPassword" placeholder="password" /> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="inputRetype">Retype</label> 
     <div class="controls"> 
      <input name="cpassword" type="password" id="inputRetype" placeholder="retype" /> 
     </div> 
    </div> 
    <div class="form-actions"> 
     <input name="signup_submit" type="submit" class="btn" value="Sign up" /> 
    </div> 
</form> 

我AUTOLOAD助手:

$autoload['helper'] = array('url', 'form');

的.htaccess:中$this->load->view('sign_up');代替redirect('user/signup');

當您加載一個視圖中顯示的驗證錯誤和它不重定向到它加載該頁面控制器方法

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/

    #Removes access to the system folder by users. 
    #Additionally this will allow you to create a System.php controller, 
    #previously this would not have been possible. 
    #'system' can be replaced if you have renamed your system folder. 
    RewriteCond %{REQUEST_URI} ^system.* 
    RewriteRule ^(.*)$ /index.php?/$1 [L] 

    #When your application folder isn't in the system folder 
    #This snippet prevents user access to the application folder 
    #Rename 'application' to your applications folder name. 
    RewriteCond %{REQUEST_URI} ^application.* 
    RewriteRule ^(.*)$ /index.php?/$1 [L] 

    #Checks to see if the user is attempting to access a valid file, 
    #such as an image or css document, if this isn't true it sends the 
    #request to index.php 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php?/$1 [L] 
</IfModule> 

<IfModule !mod_rewrite.c> 
    # If we don't have mod_rewrite installed, all 404's 
    # can be sent to index.php, and everything works as normal. 

    ErrorDocument 404 /index.php 
</IfModule> 
+0

我讀的是什麼錯誤,它真的看起來像你有成功,如果表單驗證= false。重定向不適用於表單驗證,您需要重新加載視圖,而不是重定向到它。 – 2013-02-17 15:08:23

+0

如果驗證**不**失敗,您是不是應該向數據庫添加用戶? – Shomz 2013-02-17 15:09:57

+1

當您加載視圖並且不會將其重定向到某個頁面時,會顯示驗證錯誤。嘗試加載註冊頁面的視圖,而不是重定向。讓我知道它是否有幫助。 – harsh8888 2013-02-17 15:12:31

回答