Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jhyaps's avatar

Laravel Repeater Field , Validation Error

Hello community,

I am creating a suppliers page, where I want to upload a file whenever the admin has a some file, also, i have created a type like citizenship, driving license to identify which document the suppliers has. I already setup my database

  1. first i have created a request
 public function rules()
    {
        $rules =  [
            'name' => 'required',
            'name_nepali' => 'required',
            'mobile_number' => 'required',
            'address_id' => 'required',
            'working_days_id' => 'required',
            'status_id' => 'required',
            'image' => 'mimes:jpg,jpeg,png|nullable',
            'file_name.*' => 'mimes:pdf,docs,jpg,png', // this should be nullable too if admin dont want to upload file
            'file_type_id.*' => //should be required only file_name has value other wise this should be also nullable too
            
        ];

        return $rules;
    }
  1. in my view,

by default 1 row will be shown

alt text

by clicking + button many row will be added

alt text

this above validation should be applied to all repeater row.

Plan 

At First I think disabling a select field by default and whenever the file input has value remove to disable attribute from it. but, due to the repetition of row, my code fails to run. so, is there any solution form the Laravel validation ??

If there is any thing from the Laravel validation to check each field of the repeating row that if value is present in file upload , select field should be have value, but if file upload did not contains value, I want to pass null in the database for the select attribute

Any Idea how to do it ?

0 likes
4 replies
jhyaps's avatar
jhyaps
OP
Best Answer
Level 1

@vincent15000 well, instead of using laravel custom validation rule there is a validator named required_with which I solve my issue,

here is my code if anybody needs

public function rules()
    {
        $rules =  [
            'name' => 'required',
            'name_nepali' => 'required',
            'mobile_number' => 'required',
            'address_id' => 'required',
            'working_days_id' => 'required',
            'status_id' => 'required',
            'image' => 'mimes:jpg,jpeg,png|nullable',
            'file_name.*' => 'mimes:pdf,docx,jpg,png|nullable',
            'file_type_id.*' => 'required_with:file_name.*,pdf,docx,jpg,png', // this is where i checked if the file_name has value , if it has , this file_type_id is required field.
            
        ];

        return $rules;
    }
2 likes
vincent15000's avatar

@jhyaps I think that it wasn't your question, you asked for something else : this above validation should be applied to all repeater row.

1 like
jhyaps's avatar

@vincent15000 yes, this checks too, as you see in file_name and file_type_id there is .* which indicates an array of the related field in the row.

also in my question i have mentioned that,

 'file_name.*' => 'mimes:pdf,docs,jpg,png', // this should be nullable too if admin dont want to upload file
            'file_type_id.*' => //should be required only file_name has value other wise this should be also nullable too

the file_type_id should be required only if file_name has value on it .

Any way, thank you for your reply, I may use custom validation rule some other time.

1 like

Please or to participate in this conversation.