找到你要的答案

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

Q:验证2个领域应该是相同的值,没有一个人在5名_confirmation laravel

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.

我有借方字段和借方字段

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

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

在提交表单时,我需要确保值相等。

我知道我可以这样做,如果他们这样命名:

debit 和 debit_confirmation 和 in rule array

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

但我不想改变他们的名字。

任何建立在laravel 5验证这样做。

answer1: 回答1:

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

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

Tahe a look at the validation documentation for more information

您可以使用相同的验证来匹配所需的字段

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

塔河一看更多信息的验证文档

answer2: 回答2:

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

在众多的验证器,Laravel提供了有你要找的:同。为了验证2种不同字段的值(field1,field2)的比赛,你需要定义以下规则

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

你可以看到所有可用的验证规则:HTTP:/ / laravel。COM /文档/ 5.1 /验证#可用的验证规则

php  laravel  laravel-4  laravel-5