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

[4.x]: Publishing entries: validation errors are only checked for currently selected site #11569

Closed
wsydney76 opened this issue Jul 7, 2022 · 2 comments
Assignees

Comments

@wsydney76
Copy link

wsydney76 commented Jul 7, 2022

What happened?

Description

In a multi-site environment it is possible to publish entries that contain validation errors on sites that are not currently selected.

Steps to reproduce

  1. Set up two sites
  2. Create a section with a translatable, required field
  3. Create/open an entry, leave the field blank
  4. Switch to second site
  5. Enter a value for that field
  6. Press 'Create entry/Save'

Expected behavior

Don't publish, display errors.

Actual behavior

Gets published.

Workaround

We use a workaround in a custom module like this, but not sure whether this will or will not work under any circumstances

// Validate entries on all sites
Event::on(
	Entry::class,
	Entry::EVENT_BEFORE_SAVE, function($event) {

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

	// TODO: Check conditionals

	if ($entry->scenario != Entry::STATUS_LIVE) {
		return;
	}

	$entry->validate();

	if ($entry->hasErrors()) {
		return;
	}

	foreach ($entry->getLocalized()->all() as $localizedEntry) {
		$localizedEntry->scenario = Entry::SCENARIO_LIVE;

		if (!$localizedEntry->validate()) {
			$entry->addError(
				$entry->type->hasTitleField ? 'title' : 'slug',
				Craft::t('site', 'Error validating entry in') .
				' "' . $localizedEntry->site->name . '". ' .
				implode(' ', $localizedEntry->getErrorSummary(false)));
			$event->isValid = false;
		}
	}
});

/Aylin

Craft CMS version

4.1.2 (should be the same in Craft 3)

PHP version

8.0.15

Operating system and version

Windows 10 Pro

Database type and version

MySQL 5.7.14

Image driver and version

Imagick 3.7.0 (ImageMagick 7.1.0-18)

Installed plugins and versions

@i-just i-just self-assigned this Oct 11, 2022
@i-just
Copy link
Contributor

i-just commented Oct 11, 2022

Thanks for getting in touch. We are currently discussing what would be the best approach in this case. I'll update this issue again once have something more concrete.

i-just added a commit that referenced this issue Oct 12, 2022
#11569 - WIP: validate element for all sites before publishing (live/pending/enabled)
@brandonkelly
Copy link
Member

We just released Craft 4.5.0 with a solution to this (#12125). Now all sites are validated, and if any other sites contain validation errors, the site will be mentioned in the new validation error summary.

(We ended up only implementing this for full edit pages, not slideouts, since there’s no way to actually switch to editing other sites from within slideouts.)

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