I have setup a callback validator for a Date field which means it will have an error if another field in the $context has a specific value. I have setup the validation so it works as expected for this error but doing this also triggers the The input does not appear to be a valid date.
This is the form element
$this->add([
'name' => 'DatePosted',
'type' => Date::class,
'options' => [
'label' => 'Posted Date',
],
'attributes' => [
'placeholder' => 'Date Posted',
],
]);
This is the inputFilterSpecification for that field:
'DatePosted' => [
'required' => false,
'allow_empty' => true,
'continue_if_empty' => true,
'validators' => [
[
'name' => Callback::class,
'options' => [
'messages' => [
Callback::INVALID_VALUE => 'Posted Date is required when Confirming.'
],
'callback' => function ($value, $context = []) {
$valid = true;
if ($context['Status'] == Status::COLLECTED && $value == '') {
$valid = false;
}
return $valid;
}
]
]
]
Is it possible I can have the validation fire and the field only be validated if it is populated?