在更新多条记录时,我需要检查tel字段是unique (除了id的tel)。
我知道如何使用它来验证指定的id
'tel' => 'unique:dtb_users,tel,'.$user->id但多重不是
$validator = Validator::make($request->all(), [
'tel.*' => [
'nullable',
'min:10',
'max:12',
'unique:dtb_users,tel' <- stuck here
]
]);我的表单有更多的字段,这里有两个字段(在foreach循环中)
<div class="form-group form-group-user form-user-table mb-1">
<input id="email_{{$user->id}}" name="email[{{$user->id}}]" type="email" data-id="{{ $user->id }}" class="form-control email-validation text-color{{ $errors->has("email.$user->id") ? ' is-invalid' : '' }} input_change" value="{{ $old['email'][$user->id] ?? $user->email }}" placeholder="{{__('user.email_placeholder')}}" disabled>
@if ($errors->has("email.$user->id"))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first("email.$user->id") }}</strong>
</span>
@endif
</div>
<div class="form-group form-group-user form-user-table mb-0 number">
<input id="tel_{{ $user->id }}" name="tel[{{ $user->id }}]" class="form-control tel-validation text-color{{ $errors->has('tel.' . $user->id) ? ' is-invalid' : '' }} input_change only-numbers" value="{{ $old['tel'][$user->id] ?? $user->tel }}" placeholder="00000000000" disabled>
@if ($errors->has("tel.$user->id"))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first("tel.$user->id") }}</strong>
</span>
@endif
</div>我把头发拔出来,任何想法都是值得尊重的。谢谢大家!
Answer
'unique:dtb_users,tel' -> Rule::unique('dtb_users', 'tel')->ignore(*)发布于 2022-08-30 17:59:47
我认为,在对如何命名input元素的form进行调整后,下面的内容应该可以工作。
在刀片文件中,更改name属性的值以遵循以下格式:
name="users[{{ $user->id }}][field]"是这样的;
<input id="tel_{{ $user->id }}" name="users[{{ $user->id }}][tel]"
class="form-control tel-validation text-color{{ $errors->has('tel.' . $user->id) ? ' is-invalid' : '' }} input_change only-numbers"
value="{{ $old['tel'][$user->id] ?? $user->tel }}"
placeholder="00000000000"
readonly />然后,在控制器操作中,将验证规则更新如下:
$validator = Validator::make($request->all(), [
'users' => ['required', 'array'],
'users.*.tel' => [
'nullable',
'min:10',
'max:12',
Rule::unique('users', 'tel')->ignore('*'),
]
]);发布于 2022-08-30 13:09:11
我记得在2-3年的时间里,我也和这件事做了很多斗争。以下是答案:
'tel' => 'unique:dtb_users,tel,'. $this->id .''$this->id是您要更新的记录。如果它找不到那样的话,你可以把request()->route()->parameter('paramName');
paramName是你在路线上宣布的那个;
发布于 2022-08-30 17:33:38
这条路起作用了
$validator = Validator::make($request->all(), [
'tel.*' => [
'nullable',
'min:10',
'max:12',
new UserTelUniqueRule($request->all('id', 'tel'))
]
]);在UserTelUniqueRule中
<?php
namespace App\Rules;
use App\Models\DtbUsers;
use Illuminate\Contracts\Validation\Rule;
class UserTelUnique implements Rule
{
/**
* @param array $input
*/
protected $input;
/**
* Create a new rule instance.
* @param array $input
*
* @return void
*/
public function __construct($input)
{
$this->input = $input;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
foreach ($this->input['id'] as $id) {
$tel = $this->input['tel'][$id];
if (is_null($tel)) {
continue;
}
$user = DtbUsers::where('tel', $tel)
->where('id', '!=', $id)
->whereNull('deleted_at')
->first();
if ($user) {
return false;
}
}
return true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return __('validation.unique');
}
}https://stackoverflow.com/questions/73542934
复制相似问题