0
votes

I have a question about Laravel validation. I have two number <input> fields like: enter image description here

I want to create a rule in the "Request" file from App\Requests\MyRequest.php that requires the value from the second input field to be greater than the first input field and that both fields must have a value greater than 0.

How should I code this? Does Laravel validation support this?

1

1 Answers

2
votes

You can add validations in app/Http/Requests by running php artisan make:request MyRequest something like below:

<?php

namespace App\Http\Requests;

class MyRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
    */
    public function authorize()
    {
       return true;
     }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
             'first_field' => 'min:0',
             'second_field' => 'min:'.$this->first_field,
        ];
   }
}

You can find more about validations here