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
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'
]