将请求验证文件中的输入传递给 Laravel 10 中的上一个视图应该如何实现?

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

How can I pass input from request validation file to previous view in laravel 10

问题

I made a request file in Laravel and put my validations on it but when the request redirects to the previous view, I want to pass input with redirection.

This is my request file code:

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdatePasswordRequest extends FormRequest
{

    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'password' => 'required',
            'newPassword' => [
                'required', 'string', 'min:10', 'regex:/[a-z]/', 'regex:/[A-Z]/', 'regex:/[0-9]/', 'regex:/[@$!%*#?&]/',
            ],
            'confirmPassword' => 'required',
        ];
    }

    public function messages(): array
    {
        return [
            'password.same' => 'password doesn\'t match with the current password',
            'newPassword.min' => 'new password must be at least 10 digits',
            'newPassword.regex' => "<ul><li>must contain at least one lowercase letter</li><li>must contain at least one uppercase letter</li><li>must contain at least one digit</li><li>must contain a special character for example [* # &amp; %]</li></ul>",
        ];
    }
}

I want to pass this input ['tab' => 'updatePassword']

I tried to but this function:

public function withValidator($validator)
{
    if ($validator->fails()) {
        return redirect()->back()->withInput(['tab' => 'updatePassword'])->withErrors($validator);
    }
}

And I will use old('tab') in view, but it doesn't work.

英文:

I made a request file in Laravel and put my validations on it
but when the request redirects to the previous view, I want to pass input with redirection.

This is my request file code:

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdatePasswordRequest extends FormRequest
{

    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            &#39;password&#39; =&gt; &#39;required&#39;,
            &#39;newPassword&#39; =&gt; [
                &#39;required&#39;, &#39;string&#39;, &#39;min:10&#39;, &#39;regex:/[a-z]/&#39;, &#39;regex:/[A-Z]/&#39;, &#39;regex:/[0-9]/&#39;, &#39;regex:/[@$!%*#?&amp;]/&#39;,
            ],
            &#39;confirmPassword&#39; =&gt; &#39;required&#39;,
        ];
    }
    
    public function messages(): array
    {
        return [
            &#39;password.same&#39; =&gt; &#39;password doesn\&#39;t match with the current password&#39;,
            &#39;newPassword.min&#39; =&gt; &#39;new password must be at least 10 digits&#39;,
            &#39;newPassword.regex&#39; =&gt; &quot;&lt;ul&gt;&lt;li&gt;must contain at least one lowercase letter&lt;/li&gt;
                                    &lt;li&gt;must contain at least one uppercase letter&lt;/li&gt;
                                    &lt;li&gt;must contain at least one digit&lt;/li&gt;
                                    &lt;li&gt;must contain a special character for example [* # &amp; %]&lt;/li&gt;&lt;/ul&gt;&quot;,
        ];
    }
}

I want to pass this input ['tab' => 'updatePassword']

I tried to but this function:

    public function withValidator($validator)
    {
        if ($validator-&gt;fails()) {
            return redirect()-&gt;back()-&gt;withInput([&#39;tab&#39; =&gt; &#39;updatePassword&#39;])-&gt;withErrors($validator);
        }
    }

And I will use old(&#39;tab&#39;) in view, but it doesn't work.

答案1

得分: 1

你可以尝试重写 FormRequest 类的 failedValidation() 方法。

protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
{
    throw new HttpResponseException(
        redirect()->back()
            ->withInput(['tab' => 'updatePassword'])
            ->withErrors($validator)
    );
}

<input type="hidden" name="tab" value="{{ old('tab') }}">
英文:

You can try overriding the failedValidation() method of the FormRequest class.

protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
{
    throw new HttpResponseException(
        redirect()-&gt;back()
            -&gt;withInput([&#39;tab&#39; =&gt; &#39;updatePassword&#39;])
            -&gt;withErrors($validator)
    );
}

&lt;input type=&quot;hidden&quot; name=&quot;tab&quot; value=&quot;{{ old(&#39;tab&#39;) }}&quot;&gt;

huangapple
  • 本文由 发表于 2023年4月11日 03:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979935.html
匿名

发表评论

匿名网友

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

确定