Fixed #18160 - Multi-create fixes #18247
Open
+275
−96
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As noted in #18160 you don't get the correct "serial number is invalid" message when you have a non-unique serial number while creating assets. In researching this, however, that led me down quite a rabbit hole where plenty of values weren't going to get correctly repopulated, especially on creating multiple items.
The behavior we had before was trying to create some of the items, if possible, giving a partial-success, partial-failure message. This changes the behavior so that if anything would fail, the entire submit is aborted and the fields are re-filled-out for you to make whatever changes you like (including, if you want, removing the item that can't be created). So either everything will succeed, or nothing will happen, and you get kicked back with the appropriate error messaging.
Much of the functionality is achieved with a new FormRequest that is multiple-submit-aware. But since some things can still slip through that - the database is, at the end, the ultimate arbiter - we also wrap the multiple-create step in a transaction, and rollback on any failures.
This meant I did have to create a new UniqueUndeleted validation rule, which can be used more easily in a FormRequest context.
Screenshots
Trying to specify the same (new) serial number twice
One valid asset tag, one that's already in the DB
Multiple values invalid, but one field is valid
Custom field with uniqueness constraint specified in multi-create
Fixes #18160