Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize validation messages in Laravel?

I searched a lot before asking this question but found nothing to help me. I started using Laravel as a PHP framework. As I started to do form validation, I used the Validator class to manage all my validation and error messages. But I'd like to know how I can customize the error messages (I'd like to put them in another language for example).

like image 499
Felix4 Avatar asked Aug 31 '13 00:08

Felix4


People also ask

What is custom validation in laravel?

Customizing Validation Rules Using Laravel. Share. While you are working with user input and forms, validating the common fields and extracting the exact validation rules makes the code easier to maintain and read. You can make the code powerful by using the Laravel custom validation rule with parameters.

What is the method used for specifying custom messages for validator errors in form request?

After checking if the request failed to pass validation, you may use the withErrors method to flash the error messages to the session. When using this method, the $errors variable will automatically be shared with your views after redirection, allowing you to easily display them back to the user.

How many types of validation are there in laravel?

Each form request generated by Laravel has two methods: authorize and rules .


2 Answers

The documentation says:

Language strings are stored in files within the resources/lang directory. Within this directory there should be a subdirectory for each language supported by the application.

Just create a directory with the supported language code, and add a validation.php file (it is wise to just copy the one provided in English then start translating).

An example for Spanish translation would be created at resources/lang/es/validation.php. And it will look as follow:

<?php

return array(
    "required" => "El campo :attribute es requerido.",
);

After that, you will need to change the default language.

The default language for your application is stored in the app/config/app.php configuration file. You may change the active language at any time using the App::setLocale method.

Example of changing the default language at runtime:

App::setLocale('es');
like image 60
Rubens Mariuzzo Avatar answered Oct 11 '22 18:10

Rubens Mariuzzo


in laravel 5 the validation.php file comes under the folder resources.

resources/lang/en/validation.php
like image 32
aiswarya Avatar answered Oct 11 '22 20:10

aiswarya