0

I want to customize my laravel validator.

As of now I have multiple checkboxes

<input type="checkbox" name="prodRelatedID[]" value="1">
<input type="checkbox" name="prodRelatedID[]" value="5">
<input type="checkbox" name="prodRelatedID[]" value="189">

and in my Request.php file I have this

public function rules()
{
    return [
        'prodTitle' => ['required', 'string', 'max:255'],
        'prodDesc' => ['required', 'string', 'max:255'],
        'attachment' => ['image','mimes:jpeg,png,jpg,gif,svg','max:2048'],
        'prodSize' => ['required','string','max:255'],
        'prodCategory' => ['required','string','max:255'],
        'prodPrice' =>  ['required','regex:/^\d*(\.\d{1,2})?$/'],
        'prodRelatedID' => ['required'],
        'prodRelatedID.*' => ['accepted'],
    ];
}

Now how can I customize the error message like for the prodRelatedID ?

If user didnt check the checkboxes this will return something like this

Please choose atleast 1 product related

2 Answers 2

1

There are two ways of doing this:

1 Add an array of messages to match your rules, and send it to the validate method

$messages = [
    'prodRelatedId.required' => 'Please choose at least 1 product related'
]

$request->validate($rules, $messages);

Or 2, the cleaner one, that I prefer, is to edit the

resources\assets\lang\en\validation.php

and modify the "attributes" key at the end of the file and add your values there as so:

'attributes' => [
    'prodRelatedID' => 'product related'
]
Sign up to request clarification or add additional context in comments.

Comments

1

If you're using form request then you can just overwrite the messages method:

public function messages()
{
    return [
        'prodRelatedID.required' => 'Please choose at least 1 product related',
    ];
}

Comments

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.