I'd like to return a variable message based on which one of the if
statements fails, but I don't know how to do so within one validator. I'm not quite sure what the best practice is in this case. Both of the if
statements require the $entity
, and splitting this up into separate validators would mean I'd have to query it multiple times just for the validation.
'validators' => [
[
'name' => ValidatorCallback::class,
'options' => [
'callback' => function ($value) {
$entity= $this->getObjectManager()
->getRepository(SalesChannel::class)
->find($value);
if (is_null($entity)) {
//No entity found with the given id.
return false;
}
if ($value !== $entity->getParent()->getId()) {
//Wrong parent
return false;
}
},
'messages' => [
'callbackValue' => _(
'Error, ...... is wrong'
),
],
],
],
],