Welcome Guest, Not a member yet? Register   Sign In
Why is $CI->form_validation->run() returning a bool?
#1

[eluser]Pnux[/eluser]
Im having a lot of trouble trying to figure out why form_validation->run() returns a bool. If it returned an object instead (like the database library) there could be several validations in the same page (im speculating a little on this one, didnt try this yet), keep track of the errors without needing a helper and many other goodies.

Im thinking something like:

Code:
$validation = $this->form_validation->init('foo');
$validation->set_rule('someField','required');
$result = $validation->run();
if ($result === FALSE) {
$errors = $validation->error_array;
echo json_encode(array_merge(array('success'=>false),array('error'=>$errors)));
}
else {
// do something with the data
}

Im planing on updating this post with more reasons why i think it should return an object instead of a bool, or at least be able to use it as an object. I havent started coding what i need form validation for, so all i have is a somewhat vague idea of why object is better.
#2

[eluser]smilie[/eluser]
To be honest, I do not see 'difference' between returning bool or object? In both cases, one instance of form validation can run only once. If you need another instance of form validation in same controller / function you would need to instantiate it yourself. But I can not really imagine need for this? Where / what for would you use two instances of form validation for one single function?

Cheers,
Smilie
#3

[eluser]CroNiX[/eluser]
What exactly would you return in the "object"? Either the form passes validation or it doesn't; hence a boolean result.

Not sure what you mean by "several validations on the same page". You can run all validations in the form by using form_validation::run() or execute a "group" of validation rules by form_validation::run('group_name');




Theme © iAndrew 2016 - Forum software by © MyBB