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

alierfani's avatar

Laravel 5.1 How to use the same form request rules for two methods when a field is required in one but not in the other method?

I have a from with 3 file input fields and the user should at least upload one file. I have a form request in which I'm validating them as follows:

public function rules()
    {
     $this->prepInput();
        return [
            'comment' => 'max:2000',
            'source' => 'different:target',
            'file1'=>'required_without_all:file2,file3|between:1,15360|mimes:txt,pdf',
            'file2'=>'required_without_all:file1,file3|between:1,15360|mimes:txt,pdf',
            'file3'=>'required_without_all:file1,file2|between:1,15360|mimes:txt,pdf'

        ];
    }

To update the same form, I use an update method in my controller which is almost the same as store method. The only difference is that files are not required in the update form. Is there any way to use the same form request with the store and update methods and apply the required rule optionally?

0 likes
9 replies
pmall's avatar
pmall
Best Answer
Level 56
public function rules()
{
    $rules = [
        'comment' => 'max:2000',
        'source' => 'different:target',
        'file1' => 'required_without_all:file2,file3|between:1,15360|mimes:txt,pdf',
        'file2' => 'required_without_all:file1,file3|between:1,15360|mimes:txt,pdf',
        'file3' => 'required_without_all:file1,file2|between:1,15360|mimes:txt,pdf',
    ];

    if ($this->entity_id)
    {
        $rules['file1'] = 'between:1,15360|mimes:txt,pdf';
        $rules['file2'] = 'between:1,15360|mimes:txt,pdf';
        $rules['file3'] = 'between:1,15360|mimes:txt,pdf';
    }

    return $rules;
}

Replace entity id by the name of the url parameter identifying the model you are updating. If it is a post, with post_id in the url, use $this->post_id.

3 likes
alierfani's avatar

@pmall Thank you for your answer. I replaced the entity_id with post_id but the required validation is taking place in the update form. The edit form is as follows:

  {!! Form::model($project, [
                                'route'=>['myprojects.update', $project->id],
                                'method' => 'put',
                                'files' => true,
                                'class'=>'form'
                                ]
          ) !!}
...
        <div class="form-group col-lg-4">
                {!! Form::label('file1', 'Select file 1', ['class' => 'control-label']) !!}
                {!! Form::file('file1',['id'=>'file1']) !!}
            </div>
            <div class="form-group col-lg-4">
                {!! Form::label('file2', 'Select file 2', ['class' => 'control-label']) !!}
                {!! Form::file('file2', ['id'=>'file2']) !!}
            </div>
            <div class="form-group col-lg-4">
                {!! Form::label('file3', 'Select file 3', ['class' => 'control-label']) !!}
                {!! Form::file('file3', ['id'=>'file3']) !!}
            </div>
        </div>
        <div class="form-group">
            {!! Form::submit('Edit', ['class' => 'btn btn-block btn-success']) !!}
        </div>
        {!! Form::close() !!}
pmall's avatar

I replaced the entity_id with post_id

You have to put the name of the parameter you set in the route definition. Here you want to retrieve the id of the project, if it is null it is the store action, if it is filled it is the update action, hence the if. So I guess it is id or project_id.

alierfani's avatar

@pmall I used the follwing trick and it worked but in the store method the required rule is not validated:

 if($input['_method']='PUT')
    {
        $rules['file1'] = 'between:1,15360|mimes:txt,pdf';
        $rules['file2'] = 'between:1,15360|mimes:txt,pdf';
        $rules['file3'] = 'between:1,15360|mimes:txt,pdf';
    }

    return $rules;
pmall's avatar

project_id and id are not working also.

What is your route ? Do you understand the mechanism here ?

I used the follwing trick and it worked but in the store method the required rule is not validated:

if ($input['_method'] == 'PUT') // It is double equal
alierfani's avatar

@pmall I'm kind of newbie to Laravel. I'm using resourceful routing. My route information are these: myprojects/{myprojects}/edit | myprojects.edit | App\Http\Controllers\MyProjectsController@edit

I used if($this->method()=="PUT") and it worked perfectly. Can you explain how?!

1 like
pmall's avatar

So the id of the my myproject entity is $this->myproject. If it is null it is creating a myproject, if it has a value it is updating the corresponding myproject.

The method of an update form is put so your second method works too.

If you want to access the id of the entity in the update controller action, you use $request->myproject. So it is exactly the same here, except in the request class you use $this->myproject.

2 likes

Please or to participate in this conversation.