validated 验证数组,Laravel表单请求数组验证自定义规则

I want to create a Form Request validation and don't know how-to.

I have a form:

.....

table 'users':

id | fullname | document_num

1 | John | 111

2 | Jane | 112

.. | ... | ...

when user clicks submit a request is sent to controller method where it’s first being validated by Form Request (or it can be a regular Validator).

So I want to write a rule which checks:

for (i=0; i

if (‘document_num[$i]’ exists in ‘users’ in field ‘document_num’ ) {

$user = Users::find(id of user in DB having this ‘document_num[$i]’) ;

check if (fullname[$i] == $user->fullname) {

return true} // input-ed users name match his/her name in DB.

else {return false} // input-ed users name doesn't match his/her name in DB.

}

else return true; // document_num[$i] doesn't exists in the database which's ok

if in words: check if any input-ed document_num[$i] exists in the table users, if yes, get the user having this document_nubmer from DB and compare his/her fullname value to fullname[$i] from input.

How to do it?:)

Appreciate any help!:)

解决方案

Ok. Logic of this validation in YourFormRequest is next:

Let mark all fields as required and document_num field additionaly as integer. You can add other additional constraints - it dont matter.

In rules method of YourFormRequest check in loop "is user exists for given document_num?".

If it not exists then ok - validation of this field is success.

If it exists then check "is user fullname equals for given fullname. If equals then ok - validation of this field is success. Otherwise if it fails then attach to this field your custom rule that always fails.

Let see this approach on a working example.

YourFormRequest.php

public function rules()

{

$rules = [

'fullname.*' => 'required',

'document_num.*' => 'required|integer',

];

$documentNums = request()->get('document_num');

$fullnames = request()->get('fullname');

for ($i = 0; $i < count($documentNums); $i++) {

$user = User::where('document_num', $documentNums[$i])->first();

if ($user && ($user->fullname != $fullnames[$i]) {

$rules['document_num.' . $i] = "document_num_fail:$i"; //some rule that always fails. As argument we pass a row number of field that fails

}

}

return $rules;

}

CustomValidator.php (place it for example in App\Services folder)

namespace App\Services;

class CustomValidator {

public function documentNumFailValidate($attribute, $value, $parameters, $validator) {

return false;

}

public function documentNumFailReplacer($message, $attribute, $rule, $parameters) {

return str_replace([':index'], $parameters[0], $message);

}

}

Here you can see two functions. First - to validate rule (we always pass false cause we need it). Second - it just a replacer for error message. You want to know on what field line was this error (for example on third line and fields: fullname[2] and document_num[2] respectively). As i wrote above in comment for attaching fail rule we give the number of row that fails to the validation method (documentNumFailReplacer method will replace placeholder :index in error message with the given value)

Next step - register this methods in AppServiceProvider.php

public function boot()

{

Validator::extend('document_num_fail', 'App\Services\CustomValidator@documentNumFailValidate');

Validator::replacer('document_num_fail', 'App\Services\CustomValidator@documentNumFailReplacer');

}

And final step - define your custom messages in validation.php

'custom' => [

'document_num.*' => [

'document_num_fail' => 'Input-ed user name doesn`t match his/her name in DB for specified :attribute (field position/number: :index)',

]

],

'attributes' => [

'document_num.*' => 'document number',

],

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值