If the user checks the ‘Not Required’ checkbox, then they do not have to provide a need date, so BOTH of these controls cannot be empty. I’ve tried using a callback in my model to reflect this:
$validator
->notEmptyDate('need_date', 'Please enter the Need Date, or check \'Not Required\' if no need date.', function ($context) {
if (!isset($context['data']['no_need_date'])) {
return false;
} else {
return true;
}
});
The problem: Validation fails even if I check ‘Not Required’, and the validation error message is displayed. If I change the condition to empty($context['data']['no_need_date']) or $context['data']['no_need_date'] == false, then validation gets skipped entirely. Where am I going wrong? The no_need_date field is nullable in my database and has no default value.
Could be a falsey thing. or isset($context[‘data’][‘no_need_date’] could be set with the value ‘false’ so that the isset passes. Replace your whole if statement with: -
Thanks for the advice. Unfortunately, this did not solve the problem.
Debugging shows that no_need_date has a value of ‘0’ when unchecked and a value of ‘1’ when checked. But no matter how I format my callback, it doesn’t seem to care.
(The missing end quote was a backspacing error.)
EDIT:
Got it working this way:
$validator
->notEmptyString('need_date', 'Please enter the Need Date, or check \'Not Required\' if no need date.', function ($context) {
return $context['data']['no_need_date'] === '0' ? true : false;
});
Originally, I was trying to get it to evaluate to ‘false’ to trigger the validation error, and to ‘true’ to get it to pass. Reversing them fixed it.
without the ternary operator? I would have thought the === condition would return a boolean typed value. (Old school: seeing a Boolean condition return explicitly stated true or false makes my eye twitch, hehe - I’m still sometimes lost with falsey stuff!)