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

Fix missing validation errors for meta fields in entry edit pages and slideouts #13138

Merged
merged 1 commit into from
May 2, 2023

Conversation

mmikkel
Copy link
Contributor

@mmikkel mmikkel commented Apr 30, 2023

Description

In Craft 3, it was possible to add a validation error for the typeId attribute (for example, via a custom validation rule in an Entry::EVENT_DEFINE_RULES event handler), and this error would be displayed below the entry type select field in edit pages:

CleanShot 2023-05-01 at 00 11 34@2x

In Craft 4.4.8, this error message will no longer appear and the Entry Type field will not be marked as having errors (the only thing that shows is the "Couldn't save entry" error toast).

This PR fixes this issue, by adding an 'errors' key to the to the entry type select input's config field array, with a value set to $this->getErrors('typeId').

Additionally, an 'attribute' => 'typeId' key is added to the entry type select's config array. This is required for slideouts to render the error, since slideouts use the data-attribute for showing errors, which defaults to the field's ID attribute (the Entry Type field has an ID "entryType", not "typeId").

For consistency, this PR also adds the same 'errors' key to the Parent and Author fields, even if - unlike the Entry Type field - those fields didn't actually support visible validation errors on Craft 3. (For these fields, the IDs and attributes are the same and as such there is no need for an attribute key.)

To give a bit of context and a valid use case for this PR: In the past I've often implemented restrictions related to entry types by implementing custom validation rules for the typeId attribute. As an example, the below code makes it impossible to create an entry using the entry type "Child" at the root level of a structure:

Event::on(
    Entry::class,
    Entry::EVENT_DEFINE_RULES,
    static function (DefineRulesEvent $event) {
        /** @var Entry $entry */
        $entry = $event->sender;
        $event->rules[] = ['typeId', function () use ($entry) {
            if ($entry->level === 1 && $entry->getType()->handle === 'child') {
                $entry->addError('typeId', Craft::t('site', 'Child entries must have a parent entry. Select a parent entry in the "Parent" field below.'));
            }
        }, 'on' => Entry::SCENARIO_LIVE];
    }
);

I was made aware of there not being a visible error message anymore after upgrading a project w/ similar code to Craft 4, so hopefully this PR or a different fix can be merged in. Personally, I feel like being able to surface validation errors like this is a pretty elegant way to implement custom restrictions related to entry types, and I think being able to do the same thing for the Author and Parent fields would be nice.

Related issues

@mmikkel mmikkel requested a review from a team as a code owner April 30, 2023 22:35
@brandonkelly brandonkelly merged commit d487e6b into craftcms:develop May 2, 2023
@brandonkelly
Copy link
Member

Thanks for the PR!

@brandonkelly
Copy link
Member

brandonkelly commented May 2, 2023

Craft 4.4.9 is out with those changes.

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

Successfully merging this pull request may close these issues.

2 participants