Welcome Guest, Not a member yet? Register   Sign In
Help with callback functions and form validation
#1

[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;
    }      
}
#2

[eluser]JHackamack[/eluser]
FYI:

Validation has been replaced with:
http://ellislab.com/codeigniter/user-gui...ation.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);
#3

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

[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".
#5

[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!
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB