Welcome Guest, Not a member yet? Register   Sign In
Validation Questions
#1

[eluser]jleequeen[/eluser]
I'm sure this is a problem that has been dealt with before, but I wanted to get some ideas on how to implement it with the CI Validation class.

I know the Validation class cannot possibly do everything that everyone would want, and for what it does, it serves it's purpose well. But, what I'm wondering is how to extend the validation class so that you can put other criteria in, in order for a form to validate. Let me give an example.

Let's say I have a group of fields that don't all individually need to be validated. I just basically want to make sure, of the group of fields, at least one was filled out. And let's say I also want to know, across the fields, that they add up to say 100%. So basically I have say 5 fields, and I just want to make sure at least one was filled out, and that whether one or all 5 were filled out, that the total equals 100%.

With the current validation class, I'm not sure this can be accomplished. I know you can do validation for each field, and if you want to do something custom for a field you can do a callback, but that's not the kind of validation I need. Obviously checking that all fields add up to 100% has nothing to do with an individual field rule. Hopefully you understand what I'm saying here.

Does anyone have any suggestions when it comes to custom validation and how to implement it into the validation class so that you can still choose use the...

Code:
if ($this->validation->run() == FALSE) {

}

to check for all validation rules?
#2

[eluser]garymardell[/eluser]
Regardless you can do it will a callback. Just set it on the first field in the rules section. That callback can then use the post values of the fields. Then you can set a custom error message. Alternatively you can set the same callback for each field (if you really wanted to) and have a class variable in the controller class which when running the first validation can be set as true or false depending on if its passed. Then a quick if statement can negate running the check twice and set a different error message or just not at all depending on the required functionality.
#3

[eluser]jleequeen[/eluser]
I see, that makes sense. And I have implemented the callback as you suggested and it is working just fine. I think yesterday when Colin was helping me on this question I didn't get the fact that I have to tag the callback to a field that is on the form, regardless if it actually has anything to do with that particular field. Thanks for your help.




Theme © iAndrew 2016 - Forum software by © MyBB