Validating Based on Dropdown CakePHP 3.x


I have the following validation:

    ->requirePresence('bid_id', 'create')
    ->notEmpty('bid_id', 'This field cannot be left empty', function ($context) {
        return $context['data']['type'] === 'Awarded Bid';

When I try to submit, I get this:

So the error message is correct, but it returns undefined index for some reason. I added a debug($context['data']) in the validator just before the return, and this gives output at the top of the page (the whole $context['data']-array) but right above the field - above the notice - it prints an empty array as seen below.

Why is this happening and how do I stop it?


I don’t know if always get the related type before the validation.

You can also use Hash::get to avoid the notice

return \Cake\Utility\Hash::get($context, 'data.type', false) == 'Awarded Bid';


That solved the problem. Atleast, let’s say it hides the notice.