I am trying to validate entries on Save in a Module.
My specific use case requires that the Entry validation - on failure to validate will return user to form with field errors.
On successful validation I want to use that information to call an API with the field information and if all goes well I want to save - otherwise (e.g. API error) - I want to cause validation failure and show specific errors on the form.
From My understanding: the events occur in the following order:
EVENT_BEFORE_SAVE, EVENT_BEFORE_VALIDATE, EVENT_AFTER_VALIDATE, EVENT_AFTER_SAVE
I know I can do this:
Event::on(
Entry::class,
Entry::EVENT_BEFORE_VALIDATE,
function(ModelEvent $event) {
$entry = $event->sender;
//if send to api failure
$entry->addError('field','api failed');
$event->isValid=false;
}
);
It allows me to invalidate the save but - It does so before the regular fields are validated. My desire here is that I can protect some bad inputs to the API using crafts validation features and then send to API and then act upon its result.
From what I have seen - EVENT_AFTER_VALIDATE always runs even if the field validation failed.
So It made sense to me that I could check $event->isValid in EVENT_AFTER_VALIDATE so my API hit would only happen on successful validate
I have discovered that EVENT_AFTER_VALIDATE is receiving an Event instead of a ModelEvent Which means $event->isValid cannot be used to invalidate the save or check if the validation was successful. I've tried switching this around to no avail.
I seem to be able to use $entry->addError() to invalidate the save. I'm just missing how to find out how the regular validation went..
Event::on(
Entry::class,
Entry::EVENT_AFTER_VALIDATE,
function(Event $event) {
$entry = $event->sender;
//if ($event->isValid) {
//if send to api failure
$entry->addError('field','api failed');
//}
}
);