laravel 自定义验证
One of the amazing thing about the Laravel framework is the way its doesn't get in your way while developing with it, despite the fact that it comes with great features out of the box. For most Laravel applications, the default features are fine. But there are times you would want your application to be unique by customizing (which Laravel does seamlessly) those features which are provided by default.
Laravel框架令人惊奇的一件事是,尽管它具有许多出色的功能,但在使用它进行开发时却不会妨碍您。 对于大多数Laravel应用程序,默认功能很好。 但是有时候您可能希望通过自定义(Laravel无缝执行)默认提供的功能来使应用程序唯一。
In this tutorial, I will show you how to customize validation error messages in your Laravel applications.
在本教程中,我将向您展示如何在Laravel应用程序中自定义验证错误消息。
This tutorial assumes that you are familiar with Laravel validation and will not cover it. To get up to speed with Laravel validation, you can check out our tutorial: Laravel Form Validation documentation.
本教程假定您熟悉Laravel验证,并且将不进行介绍。 为了快速掌握Laravel验证,您可以查看我们的教程: Laravel表单验证文档 。
为什么自定义验证消息 ( Why Custom Validation Messages )
But you said the default features that Laravel provides out of the box are fine? Yeah they are, depending on the type applications. But there are times you'd want your application to have a unique feel that is slightly different from a default Laravel application. That's where custom validation messages comes in. With custom validation messages, you can display validation error messages that is personalized to your application.
但是您说过Laravel提供的默认功能很好吗? 是的,取决于类型应用程序。 但是有时您希望您的应用程序具有与默认Laravel应用程序略有不同的独特感觉。 这就是定制验证消息的来源。使用定制验证消息,您可以显示针对您的应用程序个性化的验证错误消息。
设置演示联系表格 ( Setting Up The Demo Contact Form )
For the sake of this tutorial, I will be using the concept of contact form to demonstrate custom validation error messages in Laravel.
为了本教程的缘故,我将使用联系表单的概念来演示Laravel中的自定义验证错误消息。
Let's create a new Laravel application, I prefer using Laravel installer, you are free to use any method that is most convienient for you.
让我们创建一个新的Laravel应用程序,我更喜欢使用Laravel安装程序,您可以自由使用任何最方便的方法。
laravel new laravel-custom-validation-error-messages
I call this demo laravel-custom-validation-error-messages (am kinda bad with names), again feel free to use whatever name you like.
我称这个演示为laravel-custom-validation-error-messages (有点不好用名字),再一次随意使用您喜欢的名字。
Let's also create the routes for our demo contact form, add the following to your routes file:
我们还为演示联系表单创建路线,将以下内容添加到您的路线文件中:
// routes/web.php
Route::get('contact', 'ContactsController@showContactForm');
Route::post('contact', 'ContactsController@contact');
Next, let's create a controller with the respective methods to handle the routes we created.
接下来,让我们使用相应方法创建一个控制器来处理我们创建的路由。
php artisan make:controller ContactsController
Now open the ContactsController
and add the code snippets below to it:
现在打开ContactsController
并将以下代码片段添加到其中:
// app/Http/Controllers/ContactsController.php
/**
* Show the contact form
*
* @return Response
*/
public function showContactForm()
{
return view('contact');
}
The showContactForm()
simply load a view contact
(which we'll create shortly) that will show the contact form for leaving messages.
showContactForm()
只需加载一个视图contact
(我们将稍后创建),该视图contact
将显示用于留下消息的联系人表单。
Quickly, let's create a view named contact.blade.php
inside the view
directory and add the code below to it:
很快,让我们在view
目录中创建一个名为contact.blade.php
的view
,并将以下代码添加到其中:
// resources/views/contact.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact Form</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
body {
padding-top: 100px;
}
</style>
</head>
<body>
<div class="container">