0
votes

I have a function that does some validation. Instead of $errors->get(key) returning the custom error messages I've defined, I'm getting the validation rule name. For example, if I use an email that's not unique:

$messages = [
    'new_email.required' => 'Your new email address is required.',
    'new_email:unique' => 'That email is already in use.',
    'current_password|required' => 'Your current password must be provided.'
];
$rules = [
    'new_email' => 'required|email|unique:users,email,' . $user->id,
    'current_password' => 'required',
];

$validator = Validator::make($request->all(), $rules, $messages); // <-- custom error messages passed in here
if ($validator->fails()) {
    $errors = $validator->errors();
    if ($errors->has('new_email')) {
        $msg = $errors->get('new_email'); // $msg contains ['validation.unique'] instead of ['That email is already in use.']
        ...     
    }
}

$errors->get('new_email') returns ['validation.unique'] instead of the custom error message in the array that's passed as the 3rd parameter to Validator::make. How can I get the custom error message instead of the validation rule that has been broken by the request?

There are some similar questions to this, but all the answers seem to focus on the resource/lang/xx/validation.php file missing or something like that. I'm not using those localization features at all.

1
Your messages array keys are just using random characters for separators? That won't work. Use a dotmiken32

1 Answers

1
votes

Based on the documentation you should set your message with a string between property and validation rule.

$messages = [
    'new_email.unique' => 'That email is already in use.',
];