英文:
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 [* # & %]</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 [
'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>",
];
}
}
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.
答案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()->back()
->withInput(['tab' => 'updatePassword'])
->withErrors($validator)
);
}
<input type="hidden" name="tab" value="{{ old('tab') }}">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论