codeigniter表单验证不显示错误

时间:2016-02-29 13:39:27

标签: php codeigniter

我在本地计算机上使用wamp服务器来托管我的codeigniter项目。 一切似乎都运行良好,但表单验证不会在出现错误时显示错误。 检查电子邮件是否已经存在的回调也不起作用,这非常奇怪,因为它昨晚正在运行。

这是我的控制器 -

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Signup extends MX_Controller
{

function __construct() {
parent::__construct();

            //calling helpers and library classes from ci
                            $this->load->helper(array('form', 'url'));
                            $this->load->helper('date');
                            $this->load->library('form_validation');
        }

public function index() {





             // loading the registration form
                $this->load->view('register');

                // form validation
                $this->form_validation->set_rules('username', 'Username', 'required|min_length[3]|max_length[12]|is_unique[gamers.username]');
                $this->form_validation->set_rules('email','Email','trim|required|valid_email|callback_isEmailExist');
                $this->form_validation->set_rules('country', 'Country', 'required');
                $this->form_validation->set_rules('region', 'Region', 'required');
                $this->form_validation->set_rules('dob', 'Birth Date', 'required');
                $this->form_validation->set_rules('phone', 'Mobile Number', 'required');
                $this->form_validation->set_rules('passphrase', 'Password', 'trim|required|sha1');
                $this->form_validation->set_rules('referral', 'Referred By');




     if ($this->form_validation->run() !== FALSE)
                {
                     //loading model
                      $this->load->model('signup_model'); 

                    // preparing form data
                     $username  = $this->input->post('username');
                     $email     = $this->input->post('email');
                     $country   = $this->input->post('country');
                     $region    = $this->input->post('region');
                     $dob       = $this->input->post('dob');
                     $phone     = $this->input->post('phone');
                     $password  = $this->input->post('passphrase');
                     $referred  = $this->input->post('referral');
                      //check current time stamp
                     $join_date = date('Y-m-d H:i:s');
                      //email verification hush key-generator
                     $token = md5(rand(0,1000).'cashout233');


                    // packaging form data for transport in an array

                            $data = array(
                            'username'        => $username,
                            'email'           => $email,
                            'country'         => $country,
                            'region'          => $region,
                            'birthdate'       => $dob,
                            'phone_number'    => $phone,
                            'password'        => $password,
                            'join_date'       => $join_date,
                            'referral'        => $referred,
                            'token'           => $token

                );


                    // finally  transporting data to the model

                   $taxi = $this->signup_model->register_gamer($data);

                   if ($taxi !== false) {

                             // send email verification link after sucessfull transport
                  //  $from    = 'noreply@talenthut.com';
                  //  $to      =  $email; 
                  //  $subject = 'Email Confirmation Instructions'; 
                   // $message = '

                   // '.$first_name.',  Thanks for signing up!

                   // Please click this link to activate your account:
                    // localhost/index.php/email_verification?email='.$email.'&hash='.$hash.'

                   //  '; // Our message above including the link

                  //   $this->email->from($from);
                 //    $this->email->to($email);

                 //    $this->email->subject($subject);
                  //   $this->email->message($message);

                  //   $this->email->send();

                    // Redirect user to Email Confirmation Page

                    redirect('index.php/signup/EmailConfirmation/'.urlencode($email));


                    } 


                }


                }

public function isEmailExist($str) {

    $this->load->model('signup_model');
    $is_exist = $this->signup_model->isEmailExist($str);

    if ($is_exist) {
        $this->form_validation->set_message(
            'isEmailExist', 'Email address is already in use.'
        );    
        return false;
    } else {
        return true;
    }
}
public function EmailConfirmation($to)
    {

      echo "email has been sent to ".urldecode($to);
              // loading the email confirmation page
               // $this->load->view('e_confirmation');


}

}

我显示注册表单的视图是 -

<?php echo form_open('index.php/signup'); ?>

 <!-- fieldsets -->
                <fieldset>

 <div class="errors"> <?php echo validation_errors();  ?> </div>
<label>

 <input id="username" type="text" name="username" value=""  placeholder="Username" />

</label>


<label>
<input id="email" type="email" name="email" value=""  placeholder="Email" />

</label>

<label>
                    <select name="country"><br/>
                    <option value="Ghana">Ghana</option>
                    </select> 
</label>
    <label>
                    <select name="region"><br/>
                    <option value="">Choose Region...</option>
                    <option value="Greater Accra">Greater Accra</option>
                    <option value="Central">Central</option>
                    <option value="Western">Western</option>
                    <option value="Eastern">Eastern</option>
                    <option value="Ashanti">Ashanti</option>
                    <option value="Brong Ahaful">Brong Ahaful</option>
                    <option value="Northen">Northen</option>
                    <option value="Volta">Volta</option>
                    <option value="Upper East">Upper East</option>
                    <option value="Upper West">Upper West</option>
                    </select> 

    </label>

<label>

 <input id="dob" type="text" name="dob" value=""  placeholder="Birth Date" />

</label>
<label>

 <input id="phone" type="text" name="phone" value=""  placeholder="Mobile Number" />

</label>

<label>

<input id="password" type="password" name="passphrase" value=""  placeholder="Password" />

 </label>

    <label>
                    <select name="referral"><br/>
                    <option value="">how did you know about us</option>
                    <option value="Search Engine">Search engine</option>
                    <option value="Twitter">Twitter</option>
                    <option value="Word of Mouth">Word of mouth</option>
                    <option value="Newspaper">Newspaper</option>
                    </select> 

    </label>


 <label>
    <span>&nbsp;</span>
 <p class="help">By clicking the sign up button below, you confirm that you are 18 years , and you agree to our
                        <a href="/help/terms.php" target="_blank">Terms and Conditions</a> and <a href="/help/privacy.php" target="_blank">Privacy Policy</a>.</p><br/><br/>

 <span>&nbsp;</span>

<button class="submit" type="submit">Sigm Up</button>

</label> 

</fieldset>

</form>

isEmailExist控制器函数的模型函数是

function isEmailExist($email) {
    $this->db->select('id');
    $this->db->where('email', $email);
    $query = $this->db->get('gamer');

    if ($query->num_rows() > 0) {
        return true;
    } else {
        return false;
    }
}

提前致谢。

2 个答案:

答案 0 :(得分:0)

解决了!!!

我意识到验证不适用于重定向但仅适用于视图。

我还在验证前过早地调用了登录表单。我将它置于if状态,一切都像魅力一样。

关于&#34;回调&#34; 。我只需要消除所有废话并用is_unique替换它。

答案 1 :(得分:0)

您可以检查电子邮件是否存在或不仅仅是应用codeigniter inbuild规则:

<强> is_unique [table_name.table_coloum]

$this->form_validation->set_rules('email', 'Email', 'required|trim|xss_clean|is_unique[users.email]');