Louis Matthijssen
Louis Matthijssen

Reputation: 566

L5 custom validation messages not displayed

I'm using the following code in my controller action for validation. I'm 100% sure this is the code that's being used for the validation as removing and adding validations is working as expected:

$this->validate($request, [
    'email' => 'required|email',
    'password' => 'required'
], [
    'email.required' => 'Vul een e-mailadres in.',
    'email.email' => 'Vul een geldig e-mailadres in.',
    'password.required' => 'Vul een wachtwoord in.'
]);

The problem is that it keeps showing the default error messages ("The email field is required.") instead of the messages I'm providing ("Vul een e-mailadres in.").

I'm using the following code to display the errors in the view:

{{ $errors->first('email') }}

I guess this is the right way to do this, because when I choose Go To Declaration of the validate method in PhpStorm I see the following function:

vendor\laravel\framework\src\Illuminate\Foundation\Validation\ValidatesRequests.php:

public function validate(Request $request, array $rules, array $messages = array())
{
    $validator = $this->getValidationFactory()->make($request->all(), $rules, $messages);

    if ($validator->fails())
    {
        $this->throwValidationException($request, $validator);
    }
}

Why does it keep displaying the default error messages?

Upvotes: 0

Views: 2973

Answers (2)

LF-DevJourney
LF-DevJourney

Reputation: 28554

You can use the Validato::make() method.

$data = Input::all();
$rules = array(
    'email' => 'required|email',
    'password' => 'required'
);
$messages = array(
    'email.required' => 'Vul een e-mailadres in.',
    'email.email' => 'Vul een geldig e-mailadres in.',
    'password.required' => 'Vul een wachtwoord in.'
);
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails())
{
    return Response::json(['error' => $validator->errors()->first()]);
}

Upvotes: 0

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111889

The problem is, that controller validation uses ValidatesRequests and validate method is defined this way:

public function validate(Request $request, array $rules)
{
    $validator = $this->getValidationFactory()->make($request->all(), $rules);

    if ($validator->fails())
    {
        $this->throwValidationException($request, $validator);
    }
}

You cannot pass here translation. However in your case you should just create validation.php file with your translation in resources/lang/nl directory and in your config/app.php file set locale to nl

EDIT

I've looked at source and in newer Laravel 5 version, indeed 3rd parameter is used. It seems that new parameter was added and you have old version in compiled file (maybe you have modified composer.json or old composer.json file).

Whenever you have similiar issues you should run in your console:

php artisan clear-compiled

to remove compiled files.

If you don't want to have this file when development, you could remove it from composer.json file - you should remove lines with "php artisan optimize" however it will affect app performance if you don't restore it and generated new compiled.php mfile when running in production.

Upvotes: 2

Related Questions