lundi 21 octobre 2019

Validation - file name required if file upload has file

im having a form controller for an inventory, where people can add items and add bills, manuals etc too while creating the items. My problem is, that i want to get a file name too (e.g. Bill for the uploaded file) but i only want to require it, if they really have a file chosen in the form.

my code right now looks like this:

i tried everything out like required_unless, required_if but nothing works. Maybe i do sth wrong or my idea just doesnt work with this validation rules?

The validation in the Controller:

public function save(Request $request)
    {
        $validatedData = $request->validate([
            'file' => 'nullable|file',
            'files_label' => 'required_unless:file,empty|string',
        ]);

The error message in my html is: files_label has to be a string (so i guess its still required)...

my blade looks like this

<input class="form--textinput" name="files_label" id="files_label" value="">
<input type="file" class="custom-file" name="file" id="file" value="">

How can i make it work that, it just requires the file_label if i chose a file to upload?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire