英文:
Laravel - Unique validation ignore on update not working
问题
我的电子邮件唯一验证不起作用。
从这个表单中,我正在发送一个 user
<form id="edit-form" class="mt-4" action="{{ route('user.update', 1) }}" method="post">
@csrf @method('put')
</form>
它会到我的 web.php
:
Route::prefix('/user')->group(function () {
Route::put('/update/{user}', [\App\Http\Controllers\UserController::class, 'update'])->name('user.update');
});
然后它来到控制器:
public function update(UpdateUserRequest $request)
{
dd($request);
}
调用了这个请求表单:
public function rules(): array
{
return [
'email' => ['nullable', Rule::unique('users', 'email')->ignore($this->id, 'id'), 'email:rfc', 'max:255'],
];
}
但每次,如果我发送已经存在的电子邮件,它会说该电子邮件已经在使用中。
英文:
my email unique validation is not working.
From this form, I am sending a user
<form id="edit-form" class="mt-4" action="{{ route('user.update', 1) }}" method="post">
@csrf @method('put')
</form>
It goes here to my web.php
Route::prefix('/user')->group(function () {
Route::put('/update/{user}', [\App\Http\Controllers\UserController::class, 'update'])->name('user.update');
});
Then it comes here in the controller
public function update(UpdateUserRequest $request)
{
dd($request);
}
Which calls this request form:
public function rules(): array
{
return [
'email' => ['nullable', Rule::unique('users', 'email')->ignore($this->id, 'id'), 'email:rfc', 'max:255'],
];
}
Yet everytime, if I send the same email the model already has, it will say that the email is already in use.
答案1
得分: 2
在使用这个路由定义来进行更新路由时:
Route::put('update/{user}', [\App\Http\Controllers\UserController::class, 'update'])->name('user.update');
当使用 route
辅助函数时,你应该传入整个用户模型:
<form id="edit-form" class="mt-4" action="{{ route('user.update', ['user' => $user]) }}" method="post">
@csrf @method('put')
</form>
你应该在你的 UserController
上更新 update
方法:
public function update(UpdateUserRequest $request, User $user)
{
//
}
在你的 UpdateUserRequest
中,你现在可以简单地这样做:
Rule::unique('users', 'email')->ignore($this->user)
英文:
Provided that you use this route definition for your update route:
Route::put('update/{user}', [\App\Http\Controllers\UserController::class, 'update'])->name('user.update');
When using the route
helper, you should pass in an entire user model:
<form id="edit-form" class="mt-4" action="{{ route('user.update', ['user' => $user]) }}" method="post">
@csrf @method('put')
</form>
You should update the update
method on your UserController
:
public function update(UpdateUserRequest $request, User $user)
{
//
}
In your UpdateuserRequest
you will now be able to simply do this:
Rule::unique('users', 'email')->ignore($this->user)
答案2
得分: 0
在这里,它不会以注释的形式进行格式化。这不是实际解决方案,但在控制器内对我有效,尝试一下,如果有效,也应该在验证请求类中起作用。
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), [
'email' => ['nullable', Rule::unique('users', 'email')->ignore($id), 'email:rfc', 'max:255'],
]);
if ($validator->fails()) {
return response()->json($validator->errors());
}
return response('updated');
}
英文:
putting here, its not formatting in comment. Its not the actual solution but worked for me inside the controller, try it, if that works it should work in validation request class as well.
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), [
'email' => ['nullable', Rule::unique('users', 'email')->ignore($id), 'email:rfc', 'max:255'],
]);
if ($validator->fails()) {
return response()->json($validator->errors());
}
return response('updated');
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论