如何将输入从请求验证文件传递到laravel 10中的前一个视图

np8igboo  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(110)

我在Laravel中创建了一个请求文件,并将我的验证放在上面,但是当请求重定向到前一个视图时,我想通过重定向来传递输入。
这是我的请求文件代码:

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 [* # & %]</li></ul>",
        ];
    }
}

我想传递这个输入['tab' =〉'updatePassword']
我试过这个功能:

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

我将在视图中使用old('tab'),但它不起作用。

arknldoa

arknldoa1#

您可以尝试重写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') }}">

相关问题