by Devin Yang
(This article was automatically translated.)

Published - 6 years ago ( Updated - 6 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


dlaravel

D-Laravel test domain name *.dev changed to *.test

Since Google Chrome 63 will forcibly convert the domain name of *.dev to https, the domain name of the new version of D-Laravel will be changed to *.test. This article explains how to update.

dlaravel

How to set the external network IP to the Project on D-Laravel?

This video introduces how we assign Public Ip (public IP) to a specific Project in the D-Laravel environment. Whether it is the setting of the external production environment or the demo of your own project on the intranet, you can refer to the setting method of this video. In the video, although I used the external network IP of PPPoE dial-up to illustrate, we can also use the local network IP on our own computer if we change it. In this way, colleagues can connect to our D-Laravel Project.

openapi,ai,laravel

Write OpenAI test API on Laravel

Recently, everyone seems to be talking about OpenAI. If you want to play, you can write a simple API test in Laravel's API. Since you can use curl to call, you can call through guzzle. If you log in to the openai website, you can see the official teaching instructions herehttps://beta.openai.com/docs/quickstart/build-your-applicationAPI的KEY的申請, there is a button at the bottom of this page + Create new secret key