• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split Form Validation

#1
[eluser]CodeIgniterNewbie[/eluser]
I have some forms wherein the validation requires a callback to the DB, a web service, etc. Individually, these callbacks don't take up that much time. However, collectively, the do. So, I want to do the basic validations on all the form fields first -- and if those pass, only then do I do the other type of validations.

For example, say I have a field "email_address", I first want to do the following:

Code:
'field' => 'email_address',
'label' => 'Email Address',
'rules' => 'trim|required|valid_email|xss_clean'

And then do the rest of the fields in a similar way. If they all pass, then I do the validations that take a longer time.

How is this done?

#2
[eluser]kirkaracha[/eluser]
I believe the form fields and rules get evaluated in the order they appear, so as the last rule for the last form field maybe you could do a callback that does the database query, web service, etc. based on the form inputs. (The callback should be on a required field so it always gets evaluated.) Since the callback would come after all of the validation rules, the data should be valid at that point.

#3
[eluser]danmontgomery[/eluser]
You can just call one callback in form_validation, and nest the rest within that one.

Code:
function callback_function1($str) {
    if(some_validation) {
        return $this->callback_function2($str);
    }

    return false;
}

function callback_function2($str) {
    if(some_more_validation) {
        return true;
    }

    return false;
}

#4
[eluser]CodeIgniterNewbie[/eluser]
[quote author="kirkaracha" date="1266033113"]I believe the form fields and rules get evaluated in the order they appear, so as the last rule for the last form field maybe you could do a callback that does the database query, web service, etc. based on the form inputs. (The callback should be on a required field so it always gets evaluated.) Since the callback would come after all of the validation rules, the data should be valid at that point.[/quote]

Assume that there are 10 fields and each field has a callback that takes 10 seconds each. Assume that the first 9 fields all pass validation (using the 90 seconds for the callbacks). When the 10th field is validated, it turns out that it is a required field and the user forgot to populate it. 90+ seconds have gone by before it figured that out. Not good.

So, at a "field" level, this approach might work. However, at a "form" level, it doesn't.

#5
[eluser]CodeIgniterNewbie[/eluser]
[quote author="noctrum" date="1266033273"]You can just call one callback in form_validation, and nest the rest within that one.

Code:
function callback_function1($str) {
    if(some_validation) {
        return $this->callback_function2($str);
    }

    return false;
}

function callback_function2($str) {
    if(some_more_validation) {
        return true;
    }

    return false;
}
[/quote]

This might work. I'll look into it.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.