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

kapitan's avatar

Form Request = what button was clicked

I need to know which button was clicked inside a request form on laravel since i have Save and Update buttons.

App\Http\Requests\RegistrationRequest.php

public function rules()
{
    $myrule = [ 'name' => 'required', 'age' => 'required' ];
if (isset($_POST['save']))
{
    $myrule['application_form'] = 'required';
}
elseif (isset($_POST['update']))
{
    $myrule['application_form'] = 'required_without:is_application_form';
}

return $myrule;

}

i need to know if the button that was click is Save or Update because I'm requiring for the application_form file field to be required if the Save button is clicked but I only require the application_form field if the is_application_form hidden field is empty.

the setup above is working for name and age field but ignores the codes inside the IF conditions.

the thing is, this is working when placed inside the controller, just above the MyModel::create([]) code.

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

give both of your buttons the same name but different values

Then you can check the value in the request.

being within the FormRequest, you can use $this to access the values.

for instance, in your form

<button class="btn" type="Submit" value="save" name="submit">Save</button>

<button class="btn" type="Submit" value="update" name="submit">Update</button>

then in the form request

public function rules()
{
    $myrule = [ 'name' => 'required', 'age' => 'required' ];

    if ($this->submit == 'save') {

        $myrule['application_form'] = 'required';

    } elseif ($this->submit=='update') {

        $myrule['application_form'] = 'required_without:is_application_form';

    }

    return $myrule;
 
}


bit puzzled how a user chooses between save and update though?... don't they mean the same thing?

kapitan's avatar

tested and worked.

on saving, the application_form is required.

on updating, i put the value of the file name from database to is_application_form hidden field. so i only require the application_form field if the is_application_form hidden field is empty.

thanks so much!!!

Please or to participate in this conversation.