CodeIgniter Forums
form validation problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: form validation problem (/showthread.php?tid=52366)

Pages: 1 2


form validation problem - El Forum - 06-07-2012

[eluser]chubbypama[/eluser]
I have the following line in my controler:
Code:
$this->form_validation->set_rules('username','User name', 'trim|required|min_length[4]|callback_username_check');

and this is what my callback function looks like:
Code:
public function username_check($username_to_check)
{
  $this->load->model('Membership_model');
  if($query = $this->Membership_model->username_exists($username_to_check))
  {
   return FALSE; //username exists. fail the validation
  }
  else
  {
   return TRUE;
  }
}
basically, no matter what i specify for a username - aka. whether it exists or not, no matter the length, i get the same error message on the form that says "username is required". How do i know which rule it's failing on... Is my problem that i haven't specified different error messages for each rule?
I tried adding the following line to my controller but it didn't make a difference:
$this->form_validation->set_message('callback_username_check', 'username already in use.');

I tried to put some debug statements in my callback function to see whether it is returning a true or false, but the echo statements are not being displayed.

i'm sure i'm missing something really simple..
any help would be appreciated.
thanks!


form validation problem - El Forum - 06-07-2012

[eluser]chubbypama[/eluser]
i added the following line:

Code:
$this->form_validation->set_message('required', 'my custom error');

and so i guess i know now that its really failing on the "required" rule because it shows my custom error message.
I guess I can't figure out why it thinks the username is blank.
thanks


form validation problem - El Forum - 06-08-2012

[eluser]Otemu[/eluser]
Hi,

Can you paste your full controller for validation and full view so we can inspect.


form validation problem - El Forum - 06-08-2012

[eluser]chubbypama[/eluser]
Here's the code (ps. i've left in all my debug stuff)
Controller:
Code:
<?php

class Login extends CI_Controller {

public function __construct()
{
  parent::__construct();
  $this->load->library('session');

}

public function index()
{
  $data['main_content']='login_form';
  $this->load->view('includes/template',$data);

}

public function validate_credentials()
{
  $this->load->model('Membership_model');
  $query = $this->Membership_model->validate();
  if ($query) //if the user's credentials validated
  {
   $data = array(
    'username'=> $this->input->post('username'), //in config file we have turned on global xss ... so we dont need to clean up the user's data.
    'is_logged_in' => true
   );

   $this->session->set_userdata($data); //adding this data to user's session
   redirect('Site/members_area');
  }
  else
  {
   $this->index(); //redo the index function in this controller - aka reload login page.
  }

}

public function signup()
{
  $data['main_content'] = 'signup_form';
  $this->load->view('includes/template',$data);

}

public function create_member()
{

  $this->load->library('form_validation');
  //form validation:  fieldname, error message, validation rules

  $this->form_validation->set_rules('first_name', 'Name','trim|required');
  $this->form_validation->set_rules('last_name', 'Last Name','trim|required');
  $this->form_validation->set_rules('email', 'Email Address','trim|required|valid_email');
  //$this->form_validation->set_rules('username', 'Username','trim|required|min_length[4]');
  $this->form_validation->set_rules('password', 'Password','trim|required|min_length[4]|max_length[32]');
  $this->form_validation->set_rules('passwordconfirm', 'Confirm Password','trim|required|min_length[4]|max_length[32]|matches[password]');
  $this->form_validation->set_rules('callback_username_check','tHE User name', 'trim|required|min_length[3]|callback_username_check');
  $this->form_validation->set_message('username', 'username already in use.');
  $this->form_validation->set_message('required', 'my custom error');
  

  if ($this->form_validation->run() === FALSE)
  {
   $this->signup(); //validation failed.  load the same form again.
  }
  else
  {
   //load the membership model and create a new record in the database
   $this->load->model('Membership_model');

   if($query = $this->Membership_model->create_member())
   {
    //send email to administrator

    //show display    
    $data['main_content'] = 'signup_successful';
    $this->load->view('includes/template',$data);  
   }
   else
   {
    //problem adding record to database.  show the signup form again
    $data['main_content'] = 'signup_form';
    $this->load->view('includes/template',$data);
   }

  }
}

public function username_check($username_to_check)
{

  $this->load->model('Membership_model');
  if($query = $this->Membership_model->username_exists($username_to_check))
  {
   return FALSE; //username exists. fail the validation
   echo (1);
   exit;
  }
  else
  {
   return TRUE;
   echo (2);
   exit;  
  }
}
}
?>


Model:

Code:
<?php
class Membership_model extends CI_Model
{

public function __construct()
{
  //$this->load->database();
}

public function validate()
{
  $this->db->where('username', $this->input->post('username'));
  $this->db->where('password', md5($this->input->post('password')));
  $query = $this->db->get('users');

  if ($query->num_rows==1)
  {
   return true;
  }
}

public function create_member()
{
/*
  //1.  check if the user name already exists.
  $exists = $this->username_exists($this->input->post('username'));
  if ($exists)
  {
  
   return false;
  }
  else
  {
   $new_member_insert_data = array(
    'username' => $this->input->post('username'),
    'password' => md5($this->input->post('password')),
    'user_type' => 'user',
    'first_name' => $this->input->post('first_name'),
    'last_name' => $this->input->post('last_name'),
    'phone' => '1234',
    'email' => $this->input->post('email'),
    'failed_login_attempts' => 0,
    'approved_user' =>0

   );

   $insert = $this->db->insert('users',$new_member_insert_data);
   return $insert; //returns true or false
  }
*/
   $new_member_insert_data = array(
    'username' => $this->input->post('username'),
    'password' => md5($this->input->post('password')),
    'user_type' => 'user',
    'first_name' => $this->input->post('first_name'),
    'last_name' => $this->input->post('last_name'),
    'phone' => '1234',
    'email' => $this->input->post('email'),
    'failed_login_attempts' => 0,
    'approved_user' =>0

   );

   $insert = $this->db->insert('users',$new_member_insert_data);
   return $insert; //returns true or false

}

public function username_exists($username)
{
  $this->db->where('username', $username);
  $query = $this->db->get('users');

  if ($query->num_rows==1)  
  {
   //username already exists.
   return true;
  }
  else
  {
   return false;
  }
}

public function approve_user($username)
{

}

}
?>

view:


Code:
<h1>Create an Account</h1>
<fieldset>
<legend>Personal Information</legend>
&lt;?php

  //1.  fieldset is just used to logically group fields.
  //2.  set_value will remember what they typed the last time.  doen't wipe out entire form.
  
  echo form_open('Login/create_member');
  echo form_input('first_name',set_value('first_name', 'First Name'));
  echo form_input('last_name', set_value('last_name', 'Last Name'));
  echo form_input('email', set_value('email','Email Address'));
?&gt;

</fieldset>

<fieldset>
<legend>Login Info</legend>
&lt;?php
  echo form_input('username',set_value('username','Username'));
  echo form_input('password',set_value('password','Password'));
  echo form_input('passwordconfirm',set_value('passwordconfirm','Confirm Password'));
  echo form_submit('submit','Create Account');
?&gt;

&lt;?php echo validation_errors('<p class="error">'); ?&gt;
  
</fieldset>



form validation problem - El Forum - 06-08-2012

[eluser]PhilTem[/eluser]
Try to put required as first parameter of your rule and trim afterwards, i.e.

Code:
$this->form_validation->set_rules('username', 'Username','required|trim|min_length[4]');

And maybe you first wanna try without any min_length option.


form validation problem - El Forum - 06-08-2012

[eluser]chubbypama[/eluser]
tried removing the min length and putting required first:

Code:
$this->form_validation->set_rules('passwordconfirm', 'Confirm Password','trim|required|min_length[4]|max_length[32]|matches[password]');
  $this->form_validation->set_rules('callback_username_check','tHE User name', 'required|trim|callback_username_check');
  $this->form_validation->set_message('username', 'username already in use.');


but i still get the same error message that the username is required, despite supplying a user name.
actually, to be exact, the message is:

"The tHE User name field is required."


form validation problem - El Forum - 06-08-2012

[eluser]Otemu[/eluser]
I would suggest starting again with the username validation

set the rule as follows

Code:
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');

set the message as follows

Code:
$this->form_validation->set_message('username_check', 'your custom error message');

then test using sample from Codeigniter guide

Code:
public function username_check($str)
{
  if ($str == 'test')
  {
   $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
   return FALSE;
  }
  else
  {
   return TRUE;
  }
}


comment out your other code
Code:
$this->form_validation->set_rules('callback_username_check','tHE User name', 'trim|required|min_length[3]|callback_username_check');
  $this->form_validation->set_message('username', 'username already in use.');
  $this->form_validation->set_message('required', 'my custom error');

let us know if you have this working




form validation problem - El Forum - 06-08-2012

[eluser]chubbypama[/eluser]
the problem was i had:

Code:
$this->form_validation->set_rules('callback_username_check','tHE User name', 'trim|required|min_length[3]|callback_username_check');
  $this->form_validation->set_message('username', 'username already in use.');

when it should have been:

Code:
$this->form_validation->set_rules('username','tHE User name', 'trim|required|min_length[3]|callback_username_check');
  $this->form_validation->set_message('username_check', 'username already in use.');



form validation problem - El Forum - 06-08-2012

[eluser]CroNiX[/eluser]
I'd suggest setting your error message in the callback function itself.
Code:
public function username_exists($username)
{
  $this->db->where('username', $username);
  $query = $this->db->get('users');

  if ($query->num_rows==1)  
  {
   //username already exists.
   return true;
  }
  else
  {
   $this->form_validation->set_message('username_exists', 'username already in use.');
   return false;
  }
}
Although it looks like you've change the name of the method to "username_check"


form validation problem - El Forum - 06-08-2012

[eluser]Matalina[/eluser]
Your form input name is username. Your form validation is
Code:
$this->form_validation->set_rules('callback_username_check','tHE User name', 'trim|required|min_length[3]|callback_username_check');

It's going to fail because you don't have a form field called "callback_username_check"

Change it to "username" and try again.

And to the original problem:

Try

Code:
$this->form_validation->set_message('username_check', 'username already in use.');