Welcome Guest, Not a member yet? Register   Sign In
conditional validation in Model

I am transfering all my validation to my Models.
But I now have a 'problem' with a validation rule:

PHP Code:
    protected $validationRules = [
        'name'    => 'required|min_length[3]|alpha_dash|is_unique[navigations.name,id,{id}]',
        'category' => 'required_with[auto_build]',
        'depth' => 'required_with[auto_build]|less_than[4]'

It's about the DEPTH rule.
depth is only required when auto_build is present, that works.
But for the less_than I also want the validator to check for the auto_build rule.
Is is possible to have conditional validation in the Model?

Friendly bump.

Isn't there a way to do this?


You'll have to use a custom rule to test for both conditions.

1) Create a folder in your app called "Rulesets". Inside the folder, create a class file called CustomRules.php with the following basic code:

PHP Code:
Namespace App\Rulesets;

CustomRules {

2) In your model, add a use statement for the custom ruleset:

PHP Code:
use App\Rulesets\CustomRules

Now you can use custom rules in your model. Change the rule for the depth field to this:

PHP Code:
'depth' => 'isValidDepth[depth]' 

3) Next, create a function in the custom ruleset that accomplishes the validation you want. Something like this:

PHP Code:
public function isValidDepth($str$field$data, &$error) {

      $result true;

      if ( $data["auto_build"] && $str ) {
          $error "Depth must be less than 4.";
          $result false;
      elseif ( $data["auto_build"] && strlen($str) == ) {
          $error "Depth is required when auto_build is checked.";
          $result false;

      return $result;

The function assumes auto_build is a checkbox. If it's checked and the value passed to the function is greater than 4, an error is triggered. If the its checked and the value is empty, an error is triggered.

Here's an update:
1. Apparently, you don't need the use statement pointing to the custom ruleset. The model will find custom rules on its own.
2. Keep in mind that, if you have multiple sets of validation rules, you don't want to put them all in $validationRules. Any rules in that property will be applied when you call insert(), update() or save(). That is, after all, the primary purpose of the property.
3. You can put alternate sets of rules in other properties in the model, and use setRules() to set them.
4. You can do something like this in the model, but I don't think tokens in the error message get replaced:
PHP Code:
"groupname" => [
     "label" => "Service Group",
     "rules" => "in_list[COS,MAN,EST]",
     "errors" => [
         "in_list" => "The {field} field is required.",
Error messages with tokens in the $validationMessages property will get the label as expected.

Yes, it is possible to have conditional validation in the Model using the when method in CodeIgniter. You can modify your validation rule for depth as follows:

'depth' => 'required_with[auto_build]|when[auto_build,less_than[4]]'
This will ensure that the less_than[4] rule is only applied when the auto_build field is present. You can use the when method to apply any other rules conditionally as well.

Theme © iAndrew 2016 - Forum software by © MyBB