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.x]: Custom matrix block validation rule does not display custom error message #14762

Open
jamesmacwhite opened this issue Apr 5, 2024 · 0 comments

Comments

@jamesmacwhite
Copy link
Contributor

What happened?

Description

I have the following custom matrix validation implemented through the following event through a module.

Event::on(
	MatrixBlock::class,
	Model::EVENT_AFTER_VALIDATE,
	static function (Event $e) {
		$siteId = $e->sender->siteId;
		/** @var MatrixBlock $block */
		$block = $e->sender;
		$owner = $e->sender->owner;

		// Only concerned about a specific site
		if ($siteId !== 1) {
			return;
		}

		// Only if an entry and not a draft
		if ((is_a($owner, Entry::class) && ElementHelper::isDraftOrRevision($owner)) || $owner->duplicateOf) {
			return;
		}

		// Custom validation conditions related to matrix block condition
		
		...
		
                // Throw an error message if condition met
		$block->addError('links', 'Custom validation message');

	}
);

I have noticed that while the saving of the entry is correctly returned with a validation error the "Custom validation message" is not shown below the matrix block, the entry itself is prevented from saving, but no specific message is shown which is a bit awkward.

I suspect that the logic for skipping a draft is causing the no validation message problem, but I kind of don't want a draft to be prevented as this would prevent any autosave being saved, really only when an entry needs to be saved for live purposes or committed as a revision.

I don't know if there's a way I can have the best of both worlds in this scenario, but thought I'd ask.

Steps to reproduce

  1. Implement logic as above.
  2. Create an entry with a matrix block that triggers the condition
  3. Entry will be invalid, but no custom error message shown.

Expected behavior

Custom error message to be shown for the custom validation logic.

Actual behavior

No custom error message displayed, but the entry is marked as invalid.

Craft CMS version

3.9.13

PHP version

8.1

Operating system and version

No response

Database type and version

MySQL 8

Image driver and version

No response

Installed plugins and versions

N/A

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

1 participant