cms icon indicating copy to clipboard operation
cms copied to clipboard

[6.x] Asset validation error per asset

Open godismyjudge95 opened this issue 8 months ago • 6 comments

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:

image

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.

image

godismyjudge95 avatar Jul 23 '25 05:07 godismyjudge95

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.

godismyjudge95 avatar Jul 24 '25 14:07 godismyjudge95

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.

jasonvarga avatar Jul 24 '25 14:07 jasonvarga

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

godismyjudge95 avatar Jul 24 '25 14:07 godismyjudge95

Uh... I think these tests failing are unrelated? I just merged from master and now it's failing. 🫠

godismyjudge95 avatar Aug 15 '25 19:08 godismyjudge95

Don't worry about failing tests from master. We'll fix them and merge it in before reviewing 👍

duncanmcclean avatar Aug 16 '25 09:08 duncanmcclean

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'])) {

godismyjudge95 avatar Sep 22 '25 17:09 godismyjudge95