craftcms / craftcms/cms

[3.x]: Custom matrix block validation rule does not display custom error message

Open
#14,762 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug craft3
Dominant language
PHP
Stars
3.6k
Forks
705
Avg merge
1d 5h
Merged PRs (30d)
134

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the MatrixBlock::EVENT_AFTER_VALIDATE handler and the block->addError('links', ...) call shown in the report, then trace how matrix block validation errors are rendered for an entry. Reproduce the invalid entry on Craft CMS 3.9.13 and verify that the custom message appears below the matrix block without changing the reported validation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.