Skip to content
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

[3.7.x]: Setting event not valid in custom validation results in "Entry not found" #12015

Closed
LeoWie93 opened this issue Sep 27, 2022 · 3 comments
Assignees

Comments

@LeoWie93
Copy link

LeoWie93 commented Sep 27, 2022

What happened?

Description

We have a couple of websites that use their own custom validation on fields that we need to handle separately from Craft. It works perfectly on already present entries. But while creating new ones craft fails to show errors and load the edit entry view.

Things to notice

  • This is tested on a fresh install
Crafts validation triggers first
Custom validation triggers second

BUT

it only works on already saved entries. Entries that are created new will return an NotFoundHttpException: Entry not found

Custom validation to reproduce:

image

Steps to reproduce

  1. create a Module/Plugin
  2. implement a custom validation (like my example above)
  3. try to create and save a new entry that is being validated by the custom validation

Expected behavior

  • setting $event->isValid = false; will tell Craft that this element is not valid and return to the corresponding element edit page.

Actual behavior

Craft CMS version

3.7.55.2

PHP version

7.4.30

Operating system and version

Linux 5.15.70-1-lts

Database type and version

MySQL 8.0.30

Image driver and version

Imagick 3.7.0 (ImageMagick 6.9.10-23)

Installed plugins and versions

  • just my own to demonstrate a custom validaiton
@brandonkelly
Copy link
Member

Thanks for reporting that! We’ve fixed this for the next Craft 3 release via #12067.

@i-just
Copy link
Contributor

i-just commented Oct 7, 2022

In addition to Brandon's comment above, please note that once released, you can use the following code as a starting point:

Event::on(
    Entry::class,
    Element::EVENT_BEFORE_SAVE,
    function (ModelEvent $e) {

        /* @var Entry $entry */
        $entry = $e->sender;

        if (ElementHelper::isDraftOrRevision($entry)) {
            return;
        }

        if (strstr($entry->title, 'valid')) {
            return;
        }

        $entry->addError('customValidated', 'This is my error');
        $e->isValid = false;
    }
);

@brandonkelly
Copy link
Member

Craft 3.7.56 and 4.2.6 are out with the fix for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants