Hello, everyone. I have two fields in a form, one dropdown and one text:
echo $this->Form->control('label_id', ['label' => 'Sensitivity Label', 'empty' => true, 'options' => $labels]);
echo $this->Form->control('distro_notes', ['label' => 'Distribution Notes (Required if a PROPRIETARY or CONFIDENTIAL label is chosen)']);
Field distro_notes
is only required if certain label types are chosen from the dropdown. There are six available, and distro_notes
isn’t required for the first two. My validation rule looks like this:
$validator
->notEmptyString('distro_notes', 'This field is required if a PROPRIETARY or CONFIDENTIAL label is chosen', function ($context) {
$labelIds = [1,2];
return !in_array($context['data']['label_id'], $labelIds) && empty($context['data']['distro_notes']);
});
The validation executes on distro_notes
regardless of which label is chosen, when it should be allowed to remain empty if label_id
== 1 or 2. What am I missing?