Welcome Guest, Not a member yet? Register   Sign In
Issue regarding Form Validation?
#1

[eluser]Solarpitch[/eluser]
Hey Guys,

I'm trying to use the "callback_" function to check if a username is already in use and then display an error if it is. The problem is it doesnt seem to be firing the username_check function at all.

Here's a snippet of what I've got:

Code:
function index()
{
$this->load->helper(array('form', 'url'));
        
$this->load->library('validation');

$rules['name']    =  "trim|required|min_length[2]|max_length[100]";
$rules['email']    =  "trim|required|valid_email|min_length[2]|max_length[50]";
$rules['password']= "trim|required|min_length[4]|max_length[12]";
$rules['username'] = "trim|required|min_length[3]|max_length[20]|callback_username_check";

$this->validation->set_rules($rules);

if ($this->validation->run() == FALSE)
{
$this->load->view('content/home/index');
            
}
else
{
//insert user...
}
        
}




function username_check($str)
{
    
$user_check = $this->user_model->check_username($str);
$c_username = "";
        
foreach($user_check as $row)
{
echo $c_username = $row->username;
}
        
if ($str == $c_username)
{
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
#2

[eluser]Solarpitch[/eluser]
Actually... it seems to be doing something now... it's saying

"Unable to access an error message corresponding to your field name."
#3

[eluser]bretticus[/eluser]
You don't have your validation library called in your username_check method:

Code:
function username_check($str)
{
  $this->load->library('validation');
  //...
#4

[eluser]pistolPete[/eluser]
You use two different validation classes:

The old validation class in index() and the newer form validation in user_check().

You should only use the newer one:
user_guide/libraries/form_validation.html
Quote:Note: As of CodeIgniter 1.7.0, this Form Validation class supercedes the old Validation class, which is now deprecated. We have left the old class in the library so applications currently using it will not break, but you are encouraged to migrate to this new version.
#5

[eluser]Solarpitch[/eluser]
Hi Guys, I dont think either is the issue. I'm loading the new and old form validation class from the autoload file in any case.

I've seen a couple of post regarding this error but there doesnt seem to be a one stop shop solution. Spent 4 hours last night playing with things trying to fix it but I just cant.
#6

[eluser]jpi[/eluser]
As they told you the problem comes from you use 2 different class :

Code:
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
$this->validation->set_rules($rules);

Try to replace
Code:
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
By
Code:
$this->validation->set_message('username_check', 'The %s field can not be the word "test"');
#7

[eluser]grisha[/eluser]
I'll get a little off-topic with it:

Moreover make it _username_check() rather than username_check(), to make it "private" Wink
You can also make the user_model action called check_username to return a boolean (or number of affected rows), so you can get rid of the foreach loop. So I would look like:

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

[eluser]Solarpitch[/eluser]
@jpi - Ah I see... thanks, that seemed to work. Just wondering whatthe difference is between validation and form_validation. Should I be using one or the other and not both.

@Grzegorz Godlewski - Yup.. I've added the underscore to make it private and took your point about returning a boolean.

Thanks for your time guys.




Theme © iAndrew 2016 - Forum software by © MyBB