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).
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.
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.
Each form request generated by Laravel has two methods: authorize and rules .
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 theApp::setLocale
method.
Example of changing the default language at runtime:
App::setLocale('es');
in laravel 5 the validation.php file comes under the folder resources.
resources/lang/en/validation.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With