2016-02-29 1 views
0

J'utilise un serveur wamp sur ma machine locale, pour héberger mon projet codeigniter. Tout semble bien fonctionner, mais la validation du formulaire ne permet pas d'afficher les erreurs lorsqu'elles se produisent. Le rappel pour vérifier si le courrier électronique existe déjà ne fonctionne pas, ce qui est vraiment bizarre, car il fonctionnait la nuit dernière.validation de formulaire codeigniter ne pas afficher les erreurs

ceci est mon contrôleur -

<?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 = '[email protected]'; 
        // $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'); 


} 

} 

Mon point de vue qui affiche le formulaire d'inscription est -

<?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> 
fonction modèle

pour la fonction de contrôleur isEmailExist est

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

Merci à l'avance .

Répondre

0

a travaillé dehors!

J'ai réalisé que les validations ne fonctionnent pas avec les redirections mais seulement les vues. J'ai également appelé le formulaire de connexion trop tôt avant la validation. Je l'ai placé dans la condition if et tout a fonctionné comme un charme.

Comme pour le "rappel". J'ai juste dû éliminer toutes ces bêtises et l'ai remplacé par is_unique.

0

Vous pouvez vérifier le courrier électronique existe ou non comme une simple application de la règle CodeIgniter inbuild:

[table_name.table_coloum] is_unique

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