CodeIgniter Forums
Form Validation... Run always true - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Form Validation... Run always true (/thread-23307.html)



Form Validation... Run always true - El Forum - 10-06-2009

[eluser]ShoeLace1291[/eluser]
I created a member registration form last night with some rules attached. I submitted the form once and ever since then, it's been acting like its already been run everytime I visit the page, even in another browser.

http://www.srchdev.justinlosh.com/site/index.php/join

Controller:
Code:
<?php

class join extends Controller {

    function index(){
    
        $this->load->model('template');
        $this->load->helper(array('form', 'url'));
    
        $data = array(
                      'siteName' => "BetterBiz",
                      'pageTitle' => "Create an Acount",
                      'cssLoc' => "system/application/views/css/",
                      'jsLoc' => "system/application/views/js/",
                      'menuLinks' => $this->template->menuLinks()
                      );
            $this->load->view('global_header', $data);
            
        $this->load->library('form_validation');
            $this->form_validation->set_rules('loginEmail', 'Login Email', 'required|min_length[5]|valid_email');
            $this->form_validation->set_message('loginEmail', "The email address provided is not valid.");
            $this->form_validation->set_rules('displayName', 'Display Name', 'required|min_length[5]|max_length[18]');
            $this->form_validation->set_message('displayName', 'Your displayname must be between 5 and 18 characters in length.');
            $this->form_validation->set_rules('password', 'Password', 'required|min_length[7]|max_length[18]|alpha_numeric');
            $this->form_validation->set_message('pasword', "Your password must be between 7 and 18 characters in length.");
            $this->form_validation->set_rules('passConf', 'Confirm Password', 'required|matches[password]');
            $this->form_validation->set_message('passconf', 'The passwords you entered did not match.');
            $this->form_validation->set_error_delimiters('<li style="font-color: red;">', '</li>');
            
        if($this->form_validation->run() == FALSE){
        
        $data = array(
                          'error' => "Welcome!",
                          'message' => "Thank you for taking the time to join BetterBiz.com.  Before you can begin using your account, you must first activate it by opening the email we sent you and visiting the link provided."
                          );
            $this->load->view('error_page', $data);
        
        } else {
        
            $this->load->model('member');
            if($this->member->create($_POST)){
                $data['displayName'] = $_POST['displayName'];
                $this->load->view('join_success');
            } else {
                $data = array(
                              'error' => "Database Error",
                              'message' => "A database error occurred while creating your account.  Please try again later."
                              );
                $this->load->view('error_page', $data);
            }
            
            
        }
        
        $this->load->view('global_footer.php');
        
    }
    
    function emailCall($str){ //checks email availability
        
        $query = $this->db->query("SELECT * FROM members WHERE loginEmail ='".$str."'");
        if($query->num_rows() == 0){
            $this->form_validation->set_message("emailCall", "The email address you entered is already in use.");
            return FALSE;
        } else {
            return TRUE;
        }
        
    }
    
}

View:
Code:
<div id="content_container">
        <div id="content">
            <h1>Create an Account</h1>
            <hr />
            <div class="contact_form">
            <ul>
                &lt;?php echo validation_errors(); ?&gt;
            </ul>
                &lt;?php echo form_open('join'); ?&gt;

                    <label>Login Email
                        <span class="small">You will use this email address to activate your account as well as to login.</span>
                    </label>
                    &lt;input type="text" name="loginEmail" size='30'/&gt;
                                                
                    <label>Display Name
                        <span class="small">This name will display next to all the content you create, if any.</span>
                    </label>
                    &lt;input type="text" name="displayName" size='30' /&gt;
                                        
                    <label>Password
                        <span class="small">This will secure your account, so make sure that your password is something that only you will remember.</span>
                    </label>
                    &lt;input type='password' name='password' size='30' /&gt;
                    
                    <label>Confirm Password
                        <span class="small">This is to make sure that you didn't mistype the first password.</span>
                    </label>
                    &lt;input type='password' name='passconf' size='30' /&gt;
                                        
                    <button type="submit" name="submit" value="Submit">Join!</button>
                &lt;/form&gt;
            </div>
        </div>



Form Validation... Run always true - El Forum - 10-06-2009

[eluser]Mat-Moo[/eluser]
Code:
if($this->form_validation->run() == FALSE){
        
        $data = array(
                          'error' => "Welcome!",
                          'message' => "Thank you for taking the time to join BetterBiz.com.  Before you can begin using your account, you must first activate it by opening the email we sent you and visiting the link provided."
                          );
            $this->load->view('error_page', $data);
        
        } else {
        
            $this->load->model('member');
            if($this->member->create($_POST)){
                $data['displayName'] = $_POST['displayName'];
                $this->load->view('join_success');
            } else {
                $data = array(
                              'error' => "Database Error",
                              'message' => "A database error occurred while creating your account.  Please try again later."
                              );
                $this->load->view('error_page', $data);
            }
            
            
        }
Is your logic not backwards? Shouldn't it be "if($this->form_validation->run() == TRUE)" then show the thank you and tell them to check the activation email?


Form Validation... Run always true - El Forum - 10-06-2009

[eluser]Mat-Moo[/eluser]
Actually looking again, I don't understand the logic?!
Where is the code to show the form?
Should be more like IF validated - create user and send email stuff ELSE show the validation form again with errors.


Form Validation... Run always true - El Forum - 10-06-2009

[eluser]ShoeLace1291[/eluser]
Wow, I am such an idiot. I had originally had it as if form validation run was true than to create the member and if not true, then display the form. Because good php practice is to write the database code before you output html so headers don't get messed up but I guess that doesn't matter in CI... lol.