CodeIgniter Forums
Help with callback functions and form validation - 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: Help with callback functions and form validation (/showthread.php?tid=26614)



Help with callback functions and form validation - El Forum - 01-18-2010

[eluser]sdotsen[/eluser]
Code works but the username fields ignore all the other rule sets.
If I leave the field empty, it doesn't complain that the field is required.
If I input a username that exists, the function does work and will tell me that the username is already taken.

Code:
function register() {
    $this->load->library('validation');

    $fields['username'] = "trim|required|callback_username_check";
    etc ...
    etc ...

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

    if ($this->validation->run()) {

        $records = array();
        $records['username'] = $this->validation->username;
        etc ...
        etc ...

        $data = $this->account_model->registerNewAccount($records);    
    }
    $this->load->view('register_view');
}

function username_check($username) {
    $m = new Mongo();
    $collection = $m->selectDB( DBNAME )->selectCollection( TABLE );

    $data = $collection->count(array("username" => $username) );

    if($data == 1) {
        $this->validation->set_message('username_check', '%s is already taken!');
        return false;
    } else {
        return true;
    }      
}



Help with callback functions and form validation - El Forum - 01-18-2010

[eluser]JHackamack[/eluser]
FYI:

Validation has been replaced with:
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

But they also specify that the fiels are wrapped in an additional array:
Code:
$config = array(
               array(
                     'field'   => 'username',
                     'label'   => 'Username',
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'password',
                     'label'   => 'Password',
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'passconf',
                     'label'   => 'Password Confirmation',
                     'rules'   => 'required'
                  ),  
               array(
                     'field'   => 'email',
                     'label'   => 'Email',
                     'rules'   => 'required'
                  )
            );

$this->form_validation->set_rules($config);



Help with callback functions and form validation - El Forum - 01-18-2010

[eluser]sdotsen[/eluser]
Ugh ... I thought I read somewhere that form_validation was the old one. Damnit! Ok thanks for the heads up.


Help with callback functions and form validation - El Forum - 01-18-2010

[eluser]JHackamack[/eluser]
Don't worry, i keep getting confused as to which one is depreciated. Google returns the depreciated one first when you search for "Form Validation CodeIgniter".


Help with callback functions and form validation - El Forum - 01-18-2010

[eluser]sdotsen[/eluser]
[quote author="JHackamack" date="1263881320"]Don't worry, i keep getting confused as to which one is depreciated. Google returns the depreciated one first when you search for "Form Validation CodeIgniter".[/quote]

Yea, when I search on google that's what keeps coming up. Of course at the top of the wiki page, it does say to use the new "form_validation" library! haha, thats what I get for not reading.

BTW, seems like form_validation fixed my issue. Thanks a bunch!


Help with callback functions and form validation - El Forum - 01-18-2010

[eluser]JHackamack[/eluser]
Glad I could help.