nickywan123
1 week ago
120
8
Laravel

laravel validation custom message upon error?

Posted 1 week ago by nickywan123

I have a register controller that handles incoming validation request and one of my required field is based if the individual select the marital status to married and requires to fill in the input.

My controller:

  return Validator::make($data, [
                'email' => [
                    'required',
                    'string',
                    'email',
                    'max:255',
                    'unique:users'
                ],
                'password' => [
                    'required',
                    'string',
                    'min:8',
                    'confirmed'
                ],
                'full_name' => [
                    'required',
                    'string'
                ],
                'nric' => [
                    'required',
                    'min:12',
                    'max:12'
                ],
                'date_of_birth' => [
                    'required'
                ],
                'gender_id' => [
                    'required'
                ],
                'race_id' => [
                    'required'
                ],
                'marital_id' => [
                    'required'
                ],
                'address_1' => [
                    'required'
                ],

                'postcode' => [
                    'required'
                ],
                'city' => [
                    'required'
                ],
                'state' => [
                    'required'
                ],
                'contact_number_mobile' => [
                    'required',
                    'min:10'
                ],
                'contact_number_home' => [
                    'min:9'
                ],
                'spouse_full_name' => [
                    'required_if:marital_id,2'
                ],
                'employment_id' => [
                    'required'
                ],
........

When the spouse full name is not entered, the error message returns is:

The spouse full name field is required when marital id is 2.

Is there a way I can custom the message as user does not know what it means?

Please sign in or create an account to participate in this conversation.