Laravel image validataion condition in case of update help needed
$rules = [
'label' => 'required|string|max:255|unique:settings,label,'.$setting->id,
'type' => 'required|in:text,file',
'setting_value' => 'required_if:type,in:text',
'file_upload' => 'required_if:type,file|image|mimes:jpeg,png,jpg,svg|max:2048'
];
My basic validataion rule is something like above.
In form I have set a condition like: if type is selected as text it will show input field else if it is selected as 'file' then file upload button will be shown in form.
In case of store above validataion would work absolutely fine but in case of update there comes 2 different scenario:
Scenario 1:
Previously, this particular row was text type and now it is selected as file. In this case the above rules works perfectly fine.
Scenario 2:
Previously, this particular was image and now it is again selected as file, in this second case, how could I validate the image. In case of image update, I am not sending uploaded file (in case of image was already uploaded during the time of creation) so file_upload field would be empty this time, how could I bypass the required case, if the file is already uploaded.
For that, you might suggest checking setting_value if it is not empty by pass validataion else check the validation. But, currently, I am storing input value in case of type text also in setting value, so in case of update from text to file, I need to check validataion.
How could I tackle with this situation?
By the way, form field looks like this:
<div class="col-md-6 fv-row form-group {{ ($setting->type === 'file') ?'':'d-none'}}" id="file_upload">
<label class="fs-6 fw-bold mb-2" for="file">
File Upload
</label>
<input type="file" name="file_upload" onchange="loadPreview(this);" class="form-control form-control-solid @error('file_upload') is-invalid @enderror"
id="file_upload" value="" />
<img id="preview_img" src="{{asset('uploads/settings/'.$setting->setting_value)}}" class="" width="250" height="auto"/>
@error('file_upload')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
Please or to participate in this conversation.