Jump to content

Archived

This topic is now archived and is closed to further replies.

RalphLeMouf

How Do I Separate Two Validation Forms In One View With Code-Igniter

Recommended Posts

I have a login in form and a create member form on ONE view on my site in code igniter.

The problem is that when I submit the form with an error ( press submit with empty fields for the password and email on the LOGIN form, it triggers the email and password fields in the create member form and vice versa.

I have given them different submit button names and still can't get them to separate.

I have tried renaming fields,however I am confused on which values to rename as I have to submit specific values to my database.

Here is create member form and controller:

<?php

           echo form_open('auth/create_member');

           echo form_label('',  'email', array('type'=>'text'));
           $data = array( 'name' => 'first_name', 'class' => 'input', 'placeholder' => 'First Name' );
           echo form_input($data, set_value('first_name'));
           echo "<span class='errors'>";
           echo form_error('first_name');
           echo "</span>";

           echo form_label('',  'last_name', array('type'=>'text'));
           $data = array( 'name' => 'last_name', 'class' => 'input', 'placeholder' => 'Last Name' );
           echo form_input($data, set_value('last_name'));
           echo "<span class='errors'>";
           echo form_error('last_name');
           echo "</span>";

           echo form_label('',  'email', array('type'=>'text'));
           $data = array( 'name' => 'email', 'class' => 'input', 'placeholder' => 'Email' );
           echo form_input($data, set_value('email'));
           echo "<span class='errors'>";
           echo form_error('email');
           echo "</span>";

           echo form_label('',  'password', array('type'=>'password'));
           $data = array( 'name' => 'password', 'class' => 'password', 'size' => 30, 'placeholder' => 'Password'  );
           echo form_password($data, set_value('sha1(password)'));
           echo "<span class='errors'>";
           echo form_error('password');
           echo "</span";

           echo form_label('',  'password2', array('type'=>'password'));
           $data = array( 'name' => 'password2', 'class' => 'input', 'size' => 30, 'placeholder' => 'Confirm Password'  );
           echo form_password($data, set_value('sha1(password2)'));
           echo "<span class='errors'>";
           echo form_error('password2');
           echo "</span>";

           echo form_submit('submit', 'Submit');
           echo form_close();
           ?>

function create_member() 
   {
       $this->load->library('form_validation');
       $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
       $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
       $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
       $this->form_validation->set_rules('password', 'Password', 'trim|required'); 
       $this->form_validation->set_rules('password2', 'Confirm Password', 'trim|required|matches[password]');

       if($this->form_validation->run() == FALSE)
       {
           $data['main_content'] = 'home/home_page';
           $this->load->view('includes/templates/home_page_template', $data);
       }
       else 
       {
           $this->load->model('user_model');
           if($query = $this->user_model->create_member())
           {
               $this->load->model('user_model');
               $this->varification_email();
               $data['main_content'] = 'account/welcome';
               $this->load->view('includes/templates/main_page_template', $data);

           }
           else
           {
               $this->load->view('home/home_page');
           }
       }
   }

 

 

and here is the login form and controller:

<?php

               echo form_open('auth/validate_credentials_login');

               echo "<span class='errors_login'>";
               echo form_error('email');
               echo "</span>";

               echo form_label('',  'email',  array('type'=>'text'));
               $data = array( 'name' => 'email', 'class' => 'input', 'placeholder' => 'Email');
               echo form_input($data, set_value('email'));


               echo "<span class='errors_login'>";
               echo form_error('password');
               echo "</span>";

               echo form_label('',  'password', array('type'=>'password'));
               $data = array( 'name' => 'password', 'class' => 'input', 'placeholder' => 'Password');
               echo form_password($data, set_value('sha1(password)'));

               echo form_submit('submit_login', 'Login');
               echo form_close();

               ?>      
function validate_credentials_login() { // WHEN THE VIEW IS LOADED THIS FUNCTION IS CALLED AND LOADS MODEL AS WELL AS DEFINES THE SALT VARIABLE AND LOADS THE ENCRYPTING HELPER LIBRARY
       $this->load->library('encrypt');
       $this->load->helper('url');
       $this->load->library('form_validation');
       $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
       $this->form_validation->set_rules('password', 'Password', 'trim|required');
       $this->load->library('session');
       $this->load->model('user_model', 'um');
       $login = $this->input->post('submit_login');





if($login) {
               $user = $this->um->validate_home_login(array('email' => $this->input->post('email')));
               if( $user ) {

                   // CHECK THE USER'S PASSWORD AGAINST THE ONE FROM THE LOGIN FORM
                   if($user->password == $this->encrypt->sha1( $user->salt . $this->encrypt->sha1($this->input->post('password')))) {
                       $this->session->set_userdata(array(
                           'email' => $this->input->post('email')
                       ));
                       redirect('account/dashboard');
                       exit;
                   }
               }
           }

Share this post


Link to post
Share on other sites

is form_error a function native to CI or your own? Either way, it needs to know which form you are working with, and there's no argument passed to it to tell it.

 

You may be able to get around that by changing things like

$data = array( 'name' => 'email', 'class' => 'input', 'placeholder' => 'Email');
echo form_input($data, set_value('email'));

to

$data = array( 'name' => 'email_login', 'class' => 'input', 'placeholder' => 'Email');
echo form_input($data, set_value('email_login'));

(or _create, whichever)

Share this post


Link to post
Share on other sites

I'm afraid I've tried that already. I've even tried implementing that on the controller as well, but the problem is that it can't connect to the correct fields unless I were to create new fields just for that for, which I would prefer not to do.

Share this post


Link to post
Share on other sites

is form_error a function native to CI or your own? Either way, it needs to know which form you are working with, and there's no argument passed to it to tell it.

Share this post


Link to post
Share on other sites

yes I am using the native CI

form_error()

 

So I'm going to take a wild guess and say that I should create an if statement in my controller?

 

If that is the case ( which I'm almost certain it is ) what would the argument be? moreover what should I compare it to?

Share this post


Link to post
Share on other sites

You'd need some logic that tells you which form they are filling in. It can be as simple as a hidden input. Then before doing any processing, check which value was sent for that name, and then use the right logic.

Share this post


Link to post
Share on other sites

Your forms don't post to the same method, so CI will not be expecting to cross-validate. You are however, not even validating the login form.

 

You can however, rename the fields to whatever you want, as they do NOT need to be semantic with your db rows. For example, you can name your login email form "login_email" and likewise "reg_email". I would also recommend changing the error return value for your validators.

 

// from THIS in two spots
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

// to this for now
$this->form_validation->set_rules('email', 'Registration Email', 'trim|required|valid_email')

 

Furthermore, your labels are incredibly wrong. Your following:

echo form_label('',  'email',  array('type'=>'text'));

Creates this:

<label for='email' type="text"></label>

 

Do this instead

echo form_label('Form_label_text', 'form_field');

To create

<label for="form_field">Form_label_text</label>

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.