5
votes

I need to do a conditional validation on a field: if other_field = 1 then this_field = notBlank. I can not find a way to do this. My validator in table class:

    public function validationDefault(Validator $validator) {
       $validator->allowEmpty('inst_name');
       $validator->add('inst_name', [
        'notEmpty' => [
            'rule' => 'checkInstName',
            'provider' => 'table',
            'message' => 'Please entar a name.'
        ],
        'maxLength' => [
            'rule' => ['maxLength', 120],
            'message' => 'Name must not exceed 120 character length.'
        ]
    ]);
    return $validator;
}

public function checkInstName($value, array $context) {
    if ($context['data']['named_inst'] == 1) {
        if ($value !== '' && $value !== null) {
            return true;
        } else {
            return false;
        }
    } else {
        return true;
    }
}

Trouble here is, if i note, in the start of the method, that the field is allowed to be empty, when the entered value is empty, Cake doesn't run through any of my validations, because it's empty and allowed to be such. If i do not note that the field can be empty, Cake just runs "notEmpty" validation before my custom validation and outputs "This field cannot be left empty" at all time when it's empty.

How do i make Cake go through my conditional "notEmpty" validation?

I did try validation rule with 'on' condition with the same results.

1
I figured out that i could make a workaround by creating another validator. In one the inst_name would be allowed to be empty and in the other it would not be allowed to be empty. And then do the conditions in controller to use which validator. But that would be just a workaround as i would have to bring condition logic in controllers before any save actions when it should be written jut once in a model with only one validator not two.EgaSega
I deleted my answer as it didn't help. Maybe you have to use the validation on the field named_inst that has to be 1? if ($value == '1') { if (empty($context['data']['inst_name'])) { return false; } else { return true; } } Than you can try check/manipulate the errors-variable in the controller to make the message apear on field inst_name instead of named_instOops D'oh
That's a clever solution, shorter in code length than my workaround for this.EgaSega

1 Answers

1
votes

Successfully tested, this might help you and others. CakePHP 3.*

$validator->notEmpty('event_date', 'Please enter event date', function ($context) {
                if (!empty($context['data']['position'])) {
                    if ($context['data']['position'] == 1) {
                        return true; // <--  this means event date cannot be empty if position value is 1
                    }
                }
            });

In this example Event Date cannot be empty if position = 1 . You must put this condition if (!empty($context['data']['position'])) because the $context['data']['position'] value only will exist after the user click submit button. Otherwise you will get notice error.