Dynamic validation based on Select and FileUpload

Hey everyone. I have a form with a Select and a FileUpload for an image. The Select lets a user choose a location where the image must be shown. This dictates the resolution of the image I would want them to upload. Is there any way to create dynamic validation for this? So the steps would be; 1. FileUpload is disabled until a location is chosen in the Select 2. When a selection is made, I get the image_width and image_height from the Location model 3. A dynamic validation rule is made to enforce that the uploaded image adheres to that resolution I've tried combining Closure validation rules and Dependant fields, but there seems to be no way to get the current resolution of the uploaded image. Any help would be greatly appreciated.
6 Replies
Dennis Koch
Dennis Koch2y ago
1. You solved this via Closure customization, right? 2. Depending on the Location your can return a dimensions rule Be aware that Laravel validation only validates on save
jwktje
jwktjeOP2y ago
That's what I was trying to do. But I wasn't able to get the current location selected in the custom validation. How can I get the value of another field in a fields custom rules()? I tried combining the closure rule with callable $get
Dennis Koch
Dennis Koch2y ago
I guess the same as with disabled or other fields: ->rule(fn ($get) => $get('location'))
jwktje
jwktjeOP2y ago
Right! I tried using rules() I will try with rule() Yep, it was as simple as that! Awesome. This works perfectly
->rule(function (callable $get) {
$locationId = $get('location_id');
$location = Location::find($locationId);
return 'dimensions:width='.$location->image_width.',height='. $location->image_height;
})
->rule(function (callable $get) {
$locationId = $get('location_id');
$location = Location::find($locationId);
return 'dimensions:width='.$location->image_width.',height='. $location->image_height;
})
Any idea on how to easily customize the error message if it fails? I figured it out already. Thanks for the quick help @Dennis Koch . I really appreciate it!
Dennis Koch
Dennis Koch2y ago
Wanna share for anyone searching this?
astronomic
astronomic2y ago
Yes, please!
Want results from more Discord servers?
Add your server