Welcome Guest, Not a member yet? Register   Sign In
Complex validation
#1

[eluser]bewhite[/eluser]
Hello!

I have small problem with validation of my form. I have to check if at least one field from the set contains data. I have looked at the user defined functions inside validation library but I still don't understand how can I create function that will process data from several field.

Does anybody know how to do this? Maybe I have to use another CI library or class in my case?

Thanks
#2

[eluser]xwero[/eluser]
I have suggested a validation library extension to add rules in this thread and i think this is one of the rules that are common enough to put in the repository (if there ever is going to be one).
I would suggest something like
Code:
// controller
$rules['field'] = 'required_data[field1,field2]';
// rule in extension
function required_data($str,$fields)
{
    $fieldarr = explode(',',$fields);
    $errors = 0;
    foreach($fieldarr as $field)
    {
       if(!$this->required($_POST[$field])) // use of the CI validation method
       {
         $errors++;
       }
    }
    return ($errors == 0)?TRUE:FALSE
}
This is proof of concept code. Somehow it should be possible to create individual error messages for the input fields but i think you can't do this without hacking validation class.
#3

[eluser]bewhite[/eluser]
I think that the best variant for me will be my own validation. Fortunatelly, I will use it only in one place of the code so I don't need even to create a function. I don't want to dig inside standard libraries because I will have problems with CI updates later.
#4

[eluser]xwero[/eluser]
In the same thread you can find a solution for that too but the thread owner wanted another way to validate.
#5

[eluser]Michael Ekoka[/eluser]
Maybe this can answer your question.




Theme © iAndrew 2016 - Forum software by © MyBB