My required if custom validation rule |
[eluser]Ollie Rattue[/eluser]
Hello everyone, I am attempting to right a new validation rule, Required IF. I want to make one field e.g. a select field required if a corresponding field e.g. text input has a value. I have took a look at the validation class and have tried to combine two rules, matches (because it looks at two fields at once) and required, but without much success. Code: // required if another field has a value The two code igniter rules I was working from for reference purposes. Code: /** I have then added this error message to the language file. Code: $lang['requiredif'] = "The %s field is required"; I have called this like a normal validation rule. For upgradability I should extend the validation class instead of adding this rule in directly, however I wanted to remove as many possibilities for bugs. It would be fantastic if anyone could point in in the right direction, or highlight where I have gone wrong. Please note I am using code igniter CodeIgniter V 1.6.3 as I have yet to have had the time to do an upgrade to 1.70.
[eluser]lmv4321[/eluser]
Try changing your last return line in your code to the following: Code: // required if another field has a value This makes the logic work as follows: If $field is set {check $str as it is required} else return true (always fine).
[eluser]iainco[/eluser]
Sorry, made a mistake in my post and since I can't delete it I'll type this...
[eluser]t'mo[/eluser]
I made a request to get this sort of thing added to the core distribution: http://ellislab.com/forums/viewthread/110793/
[eluser]Ollie Rattue[/eluser]
[quote author="t’mo" date="1238928189"]I made a request to get this sort of thing added to the core distribution: http://ellislab.com/forums/viewthread/110793/[/quote] I wrote up my process and thoughts when I was playing with this rule at http://toomanytabs.com/requiredif.php. Maybe this page might be of interest. |
Welcome Guest, Not a member yet? Register Sign In |