我在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')
,但它不起作用。
1条答案
按热度按时间arknldoa1#
您可以尝试重写
FormRequest
类的failedValidation()
方法。