[6.x] Asset validation error per asset
This is a 6.x port of this PR - https://github.com/statamic/cms/pull/11648
It resolves:
- https://github.com/statamic/ideas/issues/1068
- https://github.com/statamic/cms/issues/10500
- https://github.com/statamic/cms/issues/6246
We've had a number of clients contact us with confusion about the validation errors that are shown when using asset validation rules. Specifically things like the dimensions rule or mime type cause confusion when it just gives a generic "invalid data" error instead of telling you what asset is invalid and why.
This PR solves that by doing three things:
- change the asset validation rules away from whole field validation rules to be single attribute Laravel rules instead
- apply the validation rules (on the assets field only) to the items in the field instead of just the field itself
- display the validation messages on each asset
In theory everything should work the same - I only had to change a single test which called the ->passes() method directly on the dimension rule.
Here is a what it looks like with invalid assets:
The asset grid does not appear to be styled for me, so I assume that is still under development. I wanted to get these changes PR'd though so whoever is working on that can incorporate them into it.
Update: the UI looks much better now.
Would you mind please keeping the scope of this PR to showing the individual asset validation errors?
Please separate the changes to the rules themselves into a separate PR. The rules we're using are based on native Laravel validation.
I believe those changes are necessary to get the assets to individually validate. The way the rules were set up currently I could not get separate validation messages for each asset. I may have overlooked a way to do so; I will double check.
Sorry I mean, the classes can change if they need to, but the logic/messages shouldn't. e.g. The dimensions rule should continue to just say "invalid image dimensions" - not give specific "must have a ratio of x" etc messages.
Sorry I mean, the classes can change if they need to, but the logic/messages shouldn't. e.g. The dimensions rule should continue to just say "invalid image dimensions" - not give specific "must have a ratio of x" etc messages.
Oh ok, yeah I can revert that
Uh... I think these tests failing are unrelated? I just merged from master and now it's failing. 🫠
Don't worry about failing tests from master. We'll fix them and merge it in before reviewing 👍
Something I noticed is that if you set a
max_files, you now get this incorrect validation error:
Good catch! I think I now have all of the whole field validation rules listed here now: https://github.com/statamic/cms/pull/11988/files#diff-22bb4c9ddfb67b736e92de38ac9fffc06904031c853cae90b1f35e270e04021eR150
if (Str::of($rule)->before(':')->is(['array', 'required', 'nullable', 'max', 'min'])) {