cms icon indicating copy to clipboard operation
cms copied to clipboard

Asset field validation fails

Open aerni opened this issue 3 years ago • 4 comments

Bug description

I've got an og_image asset field with an image validation rule. I've also got a toggle field. If the toggle is set to true, I'm generating an image in a queue on EntrySaved, save the new image path to the og_image field on the entry, and delete the old image from the drive.

Now, if I save the entry again without reloading first, I'm getting a validation error, because it's trying to validate the deleted image that is still present in the Vue data.

How to reproduce

  1. Add an asset field with image validation rule.
  2. Add an image to the field and save the entry. Make sure to stay on the page and don't reload.
  3. Now delete or rename the image file.
  4. Save the entry again and see the validation fail.

Logs

No response

Versions

Statamic 3.2.24 Pro Laravel 8.74.0 PHP 8.0.13

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

aerni avatar Dec 01 '21 13:12 aerni

I'm not sure of a solution here. Do you have any suggestions?

The only thing I can think of would be to have a JS client side thing that listens for entry saved events and updates the page. A bit overkill.

jasonvarga avatar Dec 01 '21 14:12 jasonvarga

That's the conclusion I came to as well. I looked into the Hooks but you probably can't update the Vue data with it, can you?

Another solution could be to just return true in the validation rule if no asset was found. But this might lead to other unwanted behavior.

https://github.com/statamic/cms/blob/35a71e4b31ba3b2060d889dcb208d34a2d32859a/src/Fieldtypes/Assets/ImageRule.php#L34-L36

aerni avatar Dec 01 '21 14:12 aerni

If you're cool with that validation rule then you could create your own custom one.

validate: image_if_exists

Create it using this method: https://laravel.com/docs/7.x/validation#using-extensions It's not documented in the 8.x Laravel docs, but should still work.

Validator::extend('image_if_exists', ...

jasonvarga avatar Dec 01 '21 14:12 jasonvarga

This won't work for me. I chose to do things in a different way instead.

I still think it would be cool to have a hook to change Vue data. I've been wanting to do stuff like this in a couple of cases already. Maybe move this to the ideas repo?

aerni avatar Dec 02 '21 12:12 aerni

As suggested, you could probably use a custom validation rule if you run into a similar situation in the future.

I still think it would be cool to have a hook to change Vue data. I've been wanting to do stuff like this in a couple of cases already. Maybe move this to the ideas repo?

I'm not sure I fully understand what you're asking for here... but you can update field values directly in Vuex:

this.$store.commit(`publish/base/setFieldValue`, {
    handle: 'field_handle',
    value: ['foo', 'bar'],
});

If that's not what you're after, feel free to open a feature request and we can consider adding it in the future! 😄

duncanmcclean avatar Apr 15 '24 16:04 duncanmcclean