Laravel – 更新时忽略唯一性验证不起作用

huangapple go评论125阅读模式
英文:

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

&lt;form id=&quot;edit-form&quot; class=&quot;mt-4&quot; action=&quot;{{ route(&#39;user.update&#39;, 1) }}&quot; method=&quot;post&quot;&gt;
    @csrf @method(&#39;put&#39;)
&lt;/form&gt;

It goes here to my web.php

Route::prefix(&#39;/user&#39;)-&gt;group(function () {
   Route::put(&#39;/update/{user}&#39;, [\App\Http\Controllers\UserController::class, &#39;update&#39;])-&gt;name(&#39;user.update&#39;);
});

Then it comes here in the controller

public function update(UpdateUserRequest $request)
{
    dd($request);
}

Which calls this request form:

public function rules(): array
{
    return [
        &#39;email&#39; =&gt; [&#39;nullable&#39;, Rule::unique(&#39;users&#39;, &#39;email&#39;)-&gt;ignore($this-&gt;id, &#39;id&#39;), &#39;email:rfc&#39;, &#39;max:255&#39;],
    ];
}

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(&#39;update/{user}&#39;, [\App\Http\Controllers\UserController::class, &#39;update&#39;])-&gt;name(&#39;user.update&#39;);

When using the route helper, you should pass in an entire user model:

&lt;form id=&quot;edit-form&quot; class=&quot;mt-4&quot; action=&quot;{{ route(&#39;user.update&#39;, [&#39;user&#39; =&gt; $user]) }}&quot; method=&quot;post&quot;&gt;
    @csrf @method(&#39;put&#39;)
&lt;/form&gt;

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(&#39;users&#39;, &#39;email&#39;)-&gt;ignore($this-&gt;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-&gt;all(), [
            &#39;email&#39; =&gt; [&#39;nullable&#39;, Rule::unique(&#39;users&#39;, &#39;email&#39;)-&gt;ignore($id), &#39;email:rfc&#39;, &#39;max:255&#39;],
        ]);

        if ($validator-&gt;fails()) {
            return response()-&gt;json($validator-&gt;errors());
        }

        return response(&#39;updated&#39;);
    }

huangapple
  • 本文由 发表于 2023年7月10日 17:09:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定