Setup
- Add this file to application/libraries (or, if you are already extending the Validator class, add the contents there).
- Remove Validator alias from config/application.php in order to avoid conflicts.
- Add
"required_if_attribute" => "The :attribute field is required."
to application/language/[language]/validation.php.
Usage
Define the rule using:
required_if_attribute:[field],[operator],[value]
For example:
$rules = array(
'uses_framework' => 'required',
'preferred_framework' => 'required_if_attribute:uses_framework,==,1'
);
Supported operators
- ==
- !=
- ===
- !==
- <
- <=
-
-
=
This is, of course, easily extended.
Awesome, I was writing something like this from scratch for my project, then googling brought me here, nicely coded, thanks!