advanced-nova-media-library icon indicating copy to clipboard operation
advanced-nova-media-library copied to clipboard

Validation rule required_without not working

Open TimSmitsWork opened this issue 3 years ago • 3 comments

I want to require at least 1 of my images/media but this doesn't seem to work. I think duo to the payload that is not images or video or audio but in payload this is __media__[images][], __media__[video][] and __media__[audio][]

Images::make(__('images'), 'images')->hideFromIndex()->rules('required_without_all:video,audio'),
Media::make(__('video'), 'video')->hideFromIndex()->rules('required_without_all:images,audio', 'max:1'),
Media::make(__('audio'), 'audio')->hideFromIndex()->rules('required_without_all:images,video', 'max:1'),

So when i upload an image, the video and audio keep complaining they have to be required, same when adding a video or audio.

TimSmitsWork avatar Feb 23 '22 20:02 TimSmitsWork

Does any validation work for you? I can't seem to get any sort of validation singleFile or multi working with this package.

tim-frensch avatar Apr 06 '22 08:04 tim-frensch

On Nova 4.x I see a JS error on validation on the Files field:

Uncaught TypeError: Cannot read properties of undefined (reading 'error')
    at Proxy.validateFileType (media-lib-images-field:2:148987)
    at Proxy.validateFile (media-lib-images-field:2:148497)
    at FileReader.n.onload (media-lib-images-field:2:147972)
validateFileType @ media-lib-images-field:2
validateFile @ media-lib-images-field:2
n.onload @ media-lib-images-field:2
load (async)
readFile @ media-lib-images-field:2
(anonymous) @ media-lib-images-field:2
add @ media-lib-images-field:2
n.editable.onChange.t.<computed>.t.<computed> @ media-lib-images-field:2
nn @ runtime-core.esm-bundler.js:155
rn @ runtime-core.esm-bundler.js:164
n @ runtime-dom.esm-bundler.js:366

the incriminated code snippet is this: return this.$toasted.error([...])

Any update on this?

MarDenDev avatar Apr 26 '22 12:04 MarDenDev

Thanks man, Had the same question. That payload give me the answer. It's using Laravel's built in validator. So the same rules apply when accessing the attributes from the request.

Images::make(__('images'), 'images')->hideFromIndex()->rules('required_without_all:__media__.video,__media__.audio')
Media::make(__('video'), 'video')->hideFromIndex()->rules('required_without_all:__media__.images,__media__.audio', 'max:1'),
Media::make(__('audio'), 'audio')->hideFromIndex()->rules('required_without_all:__media__.images,__media__.video', 'max:1'),

Laravel documentation on validation for nested attributes: https://laravel.com/docs/10.x/validation#a-note-on-nested-attributes

CarloDFY avatar Aug 22 '23 12:08 CarloDFY