CakePHP 4 Field Validation Problem

Hello, everyone. I have two form validation callbacks that should only be triggered under a narrow set of circumstances:

$validator
        ->scalar('rev_distro_notes')
        ->notEmptyString('rev_distro_notes', 'This field is required if a PROPRIETARY or CONFIDENTIAL label is chosen', 'create', function ($context) {
            $labelIds = [3,4,5,6];
            return in_array($context['data']['rev_label_id'], $labelIds);
        });

$validator
        ->scalar('label_change_info')
        ->notEmptyString('label_change_info', 'You must provide justification for this label change.', 'create', function ($context) {
            return $context['data']['rev_label_id'] != $context['data']['curr_label_id'];
        });

Both validations are triggering when the fields are left empty, even when the conditions I specify aren’t met, and they’re showing a generic “Please fill out this field” message. I don’t know what I’m missing here. Ideas?

That may not be the validator, on the html that control could be set as required - which may have carried over from table if it won’t allow nulls in that column and there’s no default. Note this is just a guess, but it could be the reason.

1 Like