Laravel – Validation

Tram Ho

In this article, I would like to share about validation in Laravel framework. The messages in the article are in Japanese. Go my way!

Add Validation

Validate when performing a new creation

When checking validation in laravel just list the content that we want to validate. Below is a part of the source check validation sample when registering an account.

In the source above, the check validation conditions of the fields are as follows:

admin_code is required, integer, unique

role is size

password is required, string, min, confirmed.

Apply it in practice and try the following error-making operation.

If you do not input the item Admin Code, click the button : Add/追加

Show the message already, Bravo !

By the way, I used auth’s Register.blade.php to create this screen, so the display method is also quite clear and easy to understand.

According to the Laravel Documentation, do $error defined in middleware ShareErrorsFromSession so we can use it comfortably.

Just apply to the display mechanism, we can display the error message. In addition, if a Password validation error occurs, an error message similar to an alert message will be displayed below the corresponding item.

Here, I would like to share about the error display mechanism in the code below:

In class do check if error message validation at that attribute(password) is equal to @error (Indicates where the message is displayed) , in case there is a message, display the content of @error below the corresponding item. Declare the error message at @message.

And here I will make use to display none(hide) the displayed messages.

For the Update action, do check validation only with the item that has been input

Normally, if the required item is required, the validation content will not change when doing Update Example : With the item Admin Code, although at the time of Create it is : Unique, but when performing Update, if you want to set the same as the original Admin Code at the beginning, or just Check/Update at the input state (In case the Hash PW is not displayed on the screen).

Example : I removed the unique check for admin_code. Add ‘sometimes’ and ‘nullable’ for password for the purpose of only checking when input has been performed. With just these small changes, I can handle updating that item, and only updating when the item is set to a value when saving data.

Multi-language processing of messages to Japanese

Next part I will use message in Japanese

Create Japanese files

If pre-created processing files of each language in the resources / lang directory, it will be Localization according to the settings. Folder lang The default will be as follows:

In the en folder (English), there are the following files: ・auth.php: message displayed when authen login error ・pagination.php: text displayed during pagination ・passwords.php: message displayed when password reset ・validation.php : message displayed during validation etc… When localization, will use the above file or will create it yourself.

Now I will create a folder ja to save the Japanese messgae file.

After converting to Japanese, all messages will be retrieved from the ja folder, so the current file in en will be copied to the ja folder as well.

By the way, put the multilingual files for Japanese in the same folder.

:attribute, :date will become variable.

Multi-language Item name

validation.php can set multi-language (Japanese) for the Item name displayed in the error message.

Set in array in attributes will look like this.

Message after being multilingual for Japanese

The item name displayed in the error message is converted to Japanese. Item on screen is in English, this is set in blade.php.

End !!!

Source: https://qiita.com/apricotcomic/items/acb4fdb8969990034da8#validateを追加する

Share the news now