by Devin Yang
(This article was automatically translated.)

Published - 7 years ago ( Updated - 7 years ago )

When we usually develop Laravel, sometimes field validation is performed, but the default message using Validator is in English.
But of course I want to have a more accurate display of Chinese message display,
This article describes how I use Laravel's Validator to customize Chinese messages.

1. First, import the Validator on the Controller

2. Validator can accept three fields, the third is a custom error message...
For details, see the Custom Error Messages section of the official website:

https://laravel.com/docs/5.5/validation#customizing-the-error-messages

$validator = Validator::make($input, $rules, $messages); can receive three parameters:
The first input: the value passed in by the form $request->all().
The second rule: the rule of the field, for example here, my username is required and must be alpha.
The third message: the focus of this article, a custom error message.

The following picture captures the source code in my program:

So when $validator->fails() fails to validate, it can be imported back to the form page/meeting.
If there is no verification failure, the Api will be called, the username filled in by the user will be passed in, and the URL to enter will be obtained.


3. In the code of the form blade page.
$errors->count() to see if there is an error,
Different error using foreach loop,
​​​​​​Because there may be multiple fields and multiple errors, {{$error}} is the cause of the error.

test

4. When the "Enter" button is pressed and the name is not filled in, a Chinese message of please fill in the name will appear.

5. If you only fill in 1234, press the "Enter" button, and a message " Do not fill in numbers " will appear.

demo

Tags: laravel

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


laravel,trait,php

My Browser Trait, webp image file support function detection and whether it is a mobile phone detection

I believe that many people are familiar with PHP trait, because Laravel can be seen everywhere, but I still write it and share it with those who are destined. PHP trait allows two different CLASS to use the same method. It not only reduces the complexity, but also allows the code to be reused. So it should be very convenient to put a Browser series function on Laravel's ViewServiceProvider 😝

docker, tinkerwell, tinker, laravel

Application of Tinkerwell and docker environment

In fact, I don't use Tinkerwell recently because it keeps costing me money to update. If you want to test the direct ssh host, it’s done, isn’t it?

laravel,sftp

Use Laravel's Storage SFTP Drvier for remote file upload

Why do I separate the front and back of the website? My idea is very simple, that is to rely on a set of background to control all the website data in the foreground. Assuming that the front-end website is a pure marketing website, it is nothing more than the subject content, just like the article above, without any particularly complicated logic. Therefore, it is enough to set up the backend database and connect different frontends. Then there is the last question, how can my backend HTML editor post pictures directly to the frontend? Laravel's Storage SFT Driver is a good antidote.