cms
cms copied to clipboard
Asset field validation fails
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
- Add an asset field with
imagevalidation rule. - Add an image to the field and save the entry. Make sure to stay on the page and don't reload.
- Now delete or rename the image file.
- 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
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.
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
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', ...
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?
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! 😄