I'm using laravel 11. What I want is to validate the rules in the rules()
function first before running the after()
function.
I have the following code below:
class UpdateUserRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
"first_name" => "required",
"last_name" => "required",
"user_number" => "required|exists:users,user_number",
];
}
public function after(): array
{
return [
function (Validator $validator) {
$user = User::whereUserNumber($this->user_number)->first();
dd($user);
// Some checking logic below
}
];
}
}
I followed the documentation here: .x/validation#performing-additional-validation-on-form-requests
What I want here is to validate the user_number
if it exists in the users table then perform some checking in the after
function When I entered an empty user_number
its still calling the code inside the after()
function (the value of $user
is null
).
Based on the documentation it says here that the after
method is called after validation is complete. But its not doing what I thought it to be.
Is there a elegant way to do this on laravel?
I'm using laravel 11. What I want is to validate the rules in the rules()
function first before running the after()
function.
I have the following code below:
class UpdateUserRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
"first_name" => "required",
"last_name" => "required",
"user_number" => "required|exists:users,user_number",
];
}
public function after(): array
{
return [
function (Validator $validator) {
$user = User::whereUserNumber($this->user_number)->first();
dd($user);
// Some checking logic below
}
];
}
}
I followed the documentation here: https://laravel/docs/11.x/validation#performing-additional-validation-on-form-requests
What I want here is to validate the user_number
if it exists in the users table then perform some checking in the after
function When I entered an empty user_number
its still calling the code inside the after()
function (the value of $user
is null
).
Based on the documentation it says here that the after
method is called after validation is complete. But its not doing what I thought it to be.
Is there a elegant way to do this on laravel?
Share Improve this question asked Jan 29 at 23:38 aceraven777aceraven777 4,5064 gold badges38 silver badges58 bronze badges1 Answer
Reset to default 2Based on the documentation it says here that the
after
method is called after validation is complete.
Yep, unfortunately, after
is called not after but along with other parts of this validation.
I solved this case with checking validation errors in after()
before additional checks.
This relies on MessageBag
and does not trigger repeated validation.
public function after(): array
{
return [
function (Validator $validator) {
// validation errors from previous steps will be there:
if ($validator->errors()->isNotEmpty()) {
return;
}
$user = User::whereUserNumber($this->user_number)->first();
dd($user);
// Some checking logic below
}
];
}