Welcome Guest, Not a member yet? Register   Sign In
Form Validation -> multiple fields as arguments
#1

[eluser]Ickes[/eluser]
Hello.

I want to validate a member exists based upon their submitted email and password. I would like to do this via one callback in the form validation. Can someone let me know how I could pass the 2nd field as an argument?

I know I can do...
Code:
array(
      'field'    =>    'email',
      'label'    =>    'Email',
      'rules'    =>    'callback__in_db'
      //does SELECT COUNT(*) FROM tbl WHERE email = 'field_email'
),
array(
      'field'    =>    'password',
      'label'    =>    'Password',
      'rules'    =>    'callback__in_db'
      //does SELECT COUNT(*) FROM tbl WHERE password = 'field_password'
)
and that will check each individually. But how would I allow both of them to be checked at once?

Essentially, how do I accomplish one callback that does the equivalent of
SELECT COUNT(*) FROM tbl WHERE email = 'field_email' AND password = 'field_password'?

Is it possible to do a 'rules' => 'callback__in_db[second_field_to_pass]? I've tried and can't figure out the reference for second_field_to_pass if that is possible.

Thanks in advance.
#2

[eluser]coolgeek[/eluser]
you can access the $_POST array directly
#3

[eluser]David Johansson[/eluser]
You are very close.
First, you have to decide which field to apply the rule on. Lets say you apply the rule on the email field, then you should write:
Code:
array(
      'field'    =>    'email',
      'label'    =>    'Email',
      'rules'    =>    'callback__in_db[password]'
      //does SELECT COUNT(*) FROM tbl WHERE email = 'field_email'
)

and your function _in_db could look something like:

Code:
function _in_db($email, $password_field)
{
    $password = $this->input->post($password_field);
    //Then you make your db query and return true if the result is one otherwise false
}

If you are using the build in error messages you should do something like:
Code:
$this->form_validation->set_message('_in_db', 'Wrong %s or %s.');
which would produce something like "Wrong Email or Password".




Theme © iAndrew 2016 - Forum software by © MyBB