1

I am facing the problem with custom validation messages in laravel. I want to validate user create request, I have data as follows:

first_name:Akhsay middle_name:R last_name:Doe gender:male miscellaneous:{"dob":"","birth_name":"xyz"}

I have created the validation rules for creating a user in userCreateRequest.php as

public function rules()
{
    return [
        'first_name' => 'required',
        'middle_name' => 'required',
        'last_name' => 'required',
        'gender' => 'required',
        'miscellaneous' => 'json|validate_misc_info',
    ];
}

/**
 * validation messages
 *
 * @return array
 */
public function messages()
{
    return [
        "first_name.required" => "First name is required.",
        "middle_name.required" => "Middle name is required.",
        "last_name.required" => "Last name is required.",
        "gender.required" => "Gender is required.",
        'miscellaneous.validate_misc_info' => '(**want to show specific error message here**)?'
    ];
}

For miscellaneous information, I have created a custom validation service provider and in boot method of custom validator I have written the validation code as

 Validator::extend('validate_misc_info',function($attribute, $value, $params, $validator) {

        $data = json_decode($value, true);
        $rules = [
            "dob" => "required|date",
            "birth_name" => "required|min:2|max:50",
        ];
        $messages= [
            "dob.required" => "Date of birth is required",
            "birth_name.required" => "Birth name is required",
        ];

        $validator = $data ? Validator::make($data, $rules, $messages) : false;

        if ($validator && $validator->fails()) {

            return false;
        }

        return true;
    });

But when I 'm using this validation rule in userCreateRequest.php as

public function rules()
{
    return [
        'first_name' => 'required',
        'middle_name' => 'required',
        'last_name' => 'required',
        'initial' => 'required',
        **'miscellaneous' => 'json|validate_misc_info',**
    ];
}

Its showing me the error as

{
"message": "The given data was invalid.",
"errors": {
    "miscellaneous": [
        "validation.validate_misc_info"
    ]
}

but I want to show a specific error message as

{
"message": "The given data was invalid.",
"errors": {
    "miscellaneous": [
        "Date of birth is required"
    ]
}

Please help me.

2
  • One easy way is throw new ValidationException($validator); in Validator::extend('validate_misc_info',function($attribute, $value, $params, $validator) {} when validate fails Commented Jan 11, 2019 at 7:04
  • 1
    stackoverflow.com/questions/30025252/… Commented Jan 11, 2019 at 7:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.