Welcome Guest, Not a member yet? Register   Sign In
Split the form_validation to 2 parts
#1

[eluser]eranbe[/eluser]
Hi all
I want to split the form_validation to first check the fields validity, print errors if there are any,
and only then continue to check my callbacks functions.

if I have a form_validation for log-in, I don't want to check for the username/password in the DB if the email is not valid...

Hope I managed to explain what I need,

Thanks
#2

[eluser]eranbe[/eluser]
just to make things more clear,
This is an example of the code i want to run in my controller:

$this->form_validation->set_rules('userEmail', 'email', 'required|xss_clean|trim|valid_email|min_length[6]|max_length[100]');
$this->form_validation->set_rules('userPassword', 'password','required|xss_clean|trim....')
if ($this->form_validation->run())
{
$this->form_validation->set_rules('userEmail', 'email', 'callback_check_user_and_password');
if ($this->form_validation->run())
{
...
}
}

#3

[eluser]boltsabre[/eluser]
Validation rules execute in the order that they are defined, so you could just put your callback function at the very end, and then it wont fire until all other rules have passed:

Or, you can do something along these lines:
Code:
if ($this->form_validation->run() == FALSE){
    //validation rules have failed, load view with validation errors
     $this->load->view('some_view', $data);
}else{
     //successfully passed validation, run callback
    $data['check'] = some_callback_function($input_of_whatever);
    if($check !== true){
         //load view again, $data['check'] contains the validation failed message
         $this->load->view('some_view', $data);
   }else{
      //callback worked... continue code, and redirect() to wherever the user should go.
   }
}
#4

[eluser]eranbe[/eluser]
Thanks for the answer.

I don't want to get the callback function's error message if the user did not entered mandatory field.
I don't even want to waist time to check in the DB.

As for the solution you offered, it's good but this way I'll have to add more code to my view (to deal with the data array)

So just for the others who might read this tread,
I tried the code I wrote above and it worked fine.

If any mandatory fields are missing, I get error messages only for them.
only if the first validation pass, I re-set the rule for my callback function and check form_validation->run() again.
#5

[eluser]CroNiX[/eluser]
See the form validation userguide in the "Creating Sets of Rules" section. You can define a named set of rules and execute that set of rules - so you execute your initial rules there. If it passes, then you can execute a second set/group where it would do the db stuff.
#6

[eluser]eranbe[/eluser]
Thanks CroNiX.
Missed it in the guide. this is exactly what I need.




Theme © iAndrew 2016 - Forum software by © MyBB