Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate 2 fields should be the same value without one of them has _confirmation in its name in laravel 5

I have debit field and debit field

 <input type="text" class="form-control" name="debit" value="{{ isset($expense->debit) ? $expense->debit : old('debit')}}"> 

and

 <input type="text" class="form-control" name="credit" value="{{ isset($expense->credit) ? $expense->credit: old('credit')}}"> 

i need to make sure the values are equal when submit the form.

i know I can do that if name them like this:

debit and debit_confirmation and in rule array

return Validator::make($data, [
        'debit' => 'required|confirmed',
    ]);

but I don't want to change their names.

any build-in validation in laravel 5 do that.

like image 763
Ayman Hussein Avatar asked Dec 08 '22 02:12

Ayman Hussein


2 Answers

You could use the same validation rule to match the fields you want.

return Validator::make($data, [
    'debit' => 'required|same:credit',
]);

You could take a look at the laravel documentation for more information about validation rules

like image 101
Dimitri Acosta Avatar answered Dec 26 '22 11:12

Dimitri Acosta


Among many validators that Laravel offers there is one you're looking for: same. In order to validate if values of 2 different fields (field1, field2) match, you need to define the following rules

 $rules = [
  'field1' => 'same:field2'
];

You can see a list of all available validation rules here: http://laravel.com/docs/5.1/validation#available-validation-rules

like image 28
jedrzej.kurylo Avatar answered Dec 26 '22 09:12

jedrzej.kurylo