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] Add validation to prevent duplicate field handles #9337

Merged
merged 2 commits into from
Mar 7, 2024

Conversation

duncanmcclean
Copy link
Member

@duncanmcclean duncanmcclean commented Jan 16, 2024

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
Member

We already have validation on the blueprint level:

public function validateUniqueHandles()
{
$fields = $this->fieldsCache ?? new Fields($this->tabs()->map->fields()->flatMap->items());
$handles = $fields->resolveFields()->map->handle();
if ($field = $handles->duplicates()->first()) {
throw new DuplicateFieldException($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. 👍

@jasonvarga jasonvarga merged commit c50d71a into 4.x Mar 7, 2024
35 checks passed
@jasonvarga jasonvarga deleted the prevent-duplicate-field-handles branch March 7, 2024 22:08
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.

Error when searching in the Link a single field box
2 participants