-
Notifications
You must be signed in to change notification settings - Fork 642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[4.0.3]: Custom validation causes an error saving a provisional draft #11407
Comments
Sorry for the delayed response. I think the issue here is the way you are calling your public function init()
Event::on(
Entry::class,
Entry::EVENT_AFTER_VALIDATE,
function(Event $e) {
$this->validateScheduleEntry($e);
}
);
parent::init();
} |
@brandonkelly Thanks for the response! But this is not the issue. I'm not calling the function directly, the three dots are the first class callable syntax introduced in PHP 8.1. The notation Any other ideas what might be causing this? |
Ah, TIL! So the issue is most likely that you are skipping validation for drafts and revisions: if (ElementHelper::isDraftOrRevision($entry)) return; That means the validation won’t run when Craft is validating the provisional draft. Craft assumes that if the provisional draft validates with the Maybe change that condition to: if (
$entry->scenario !== Entry::SCENARIO_LIVE &&
ElementHelper::isDraftOrRevision($entry)
) {
return;
} |
@brandonkelly Thank you, that solves the issue indeed! Adding the scenario condition also got rid of the duplicated errors, now the error messages show only once as expected. I still don't fully understand this fix though. I thought a provisional draft was an unsaved draft for an existing entry. When I click on "New entry" in the entries list, does this already create a provisional draft? And just so I get it right: When I have finished my draft and click on "Create entry", Craft first validates the draft with SCENARIO_LIVE, then saves the draft as a published entry and validates it again? So the validation is essentially done twice and this is why I was seeing duplicate error messages? I'm a bit confused now, would you mind explaining this in a bit more detail? Thanks! |
When you first create an entry, you are working with an “unpublished draft” – which is an a la carte draft; it doesn’t reference a “real” entry like other drafts do. When you press “Create entry” on the unpublished draft, the draft is validated with the When you apply a normal/provisional draft to an entry, and the draft is enabled, first the draft will be validated with the |
@brandonkelly Thanks for the explanation! But if unpublished drafts are validated as drafts with the |
Sorry, I got the details a little wrong in my last comment. Validation is in fact run twice when saving an unpublished draft – once when updating the draft with the post data, and again when removing its draft status. (Comment is updated to reflect that.) I looked into the error you were getting because I realized it didn’t make any sense – it was coming from the |
@brandonkelly Awesome, thanks for the explanation, and for taking the time to explain this in detail! Got my hook working exactly right now, it's not throwing the exception anymore and only displaying my validation messages once. Good catch regarding the bug, looking forward to the next release! 💯 |
Glad you’re sorted! Craft 4.2.2 is out now with those two fixes. Thanks for the persistence :) |
What happened?
Description
I'm trying to add some custom validation rules to an entry, but I'm getting some errors when first saving an entry.
Here's a shortened version of my custom validation code that lives inside a module:
Now this works fine for entries that are already published. But for new entries, it causes errors:
date_start
field. As a sidenote, the error is displayed twice at this point, any ideas why?Here's the full stack trace:
In the line that the error is coming from, the
ElementsController
checks if the post data specifies an element and that element is not a draft or revision. I tried to debug this to find out more – at the point that the error is thrown,$element->getIsDraft()
returns true. So the controller is expecting a published entry at that point but the element is still a draft.Not sure if this is a bug with the entry creation flow or if I'm just attaching my validation rules in a wrong way. I also looked at the
defineRules
event, but it's much more involved and verbose, so I prefer the validation and/or save hooks.Steps to reproduce
Craft CMS version
4.0.3
PHP version
8.1
Operating system and version
macOS 12.2
Database type and version
MySQL 8
Image driver and version
No response
Installed plugins and versions
No response
The text was updated successfully, but these errors were encountered: