Skip to content

[4.x] Add validation to prevent duplicate field handles - #9337

Merged
jasonvarga merged 2 commits into
4.xfrom
prevent-duplicate-field-handles
Mar 7, 2024
Merged

[4.x] Add validation to prevent duplicate field handles#9337
jasonvarga merged 2 commits into
4.xfrom
prevent-duplicate-field-handles

Conversation

@duncanmcclean

@duncanmccleanduncanmcclean commented Jan 16, 2024

Copy link
Copy Markdown
Member

This pull request implements a validation rule to prevent adding multiple fields to a blueprint with the same handle.

Previous to this PR, blueprints & fieldsets behaved differently to duplicate field handles:

  • When you saved a blueprint where multiple fields share a handle, only one of the fields would actually be saved.
  • When you saved a fieldset where multiple fields share a handle, both of the fields would be saved, causing issues like that seen in Error when searching in the Link a single field box #9333.

Now, with this PR, when you attempt to save or update a field using the same handle as another field, you'll see a validation error:

CleanShot 2024-01-16 at 16 43 07

The validation will check both actual fields in the blueprint as well as any fields imported via fieldsets.

Closes#9333.

@jasonvarga

Copy link
Copy Markdown
Member

We already have validation on the blueprint level:

publicfunctionvalidateUniqueHandles()
{
$fields = $this->fieldsCache ?? newFields($this->tabs()->map->fields()->flatMap->items());
$handles = $fields->resolveFields()->map->handle();
if ($field = $handles->duplicates()->first()) {
thrownewDuplicateFieldException($field, $this);
}
}

But, at some point I guess something changed and it doesn't work quite right. It looks like by the time it gets to line 619, the duplicates are already gone, so the exception doesn't get thrown. (Line 615 changed as part of #7746. It was probably that.)

Regardless, even if it worked, the validation error only happens when you save the entire blueprint.

This PR is a nice addition because it'll prevent you from putting a duplicate into the field stack in the first place. 👍

Sign up for freeto 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.

Error when searching in the Link a single field box

2 participants

@duncanmcclean@jasonvarga