未加星标

Deep Dive Into Custom Validation Error Messages In Laravel

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Code

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.

In this tutorial, I will show you how to customize validation error messages in your Laravel applications.

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 .

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.

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.

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 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.

Let's also create the routes for our demo contact form, add the following to your routes file:

// routes/web.php Route::get('contact', [email protected]'); Route::post('contact', [email protected]');

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:

// 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.

Quickly, let's create a view named contact.blade.php inside the view directory and add the code below to it:

// 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"> <div class="row"> <div class="col-md-6 col-md-offset-3"> @if (session('status')) <div class="alert alert-success"> {{ session('status') }} </div> @endif <form action="{{ url('contact') }}" method="POST"> {{ csrf_field() }} <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}"> <label for="name">Name</label> <input type="text" name="name" class="form-control" value="{{ old('name')}}"> <small class="text-danger">{{ $errors->first('name') }}</small> </div> <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}"> <label for="email">Email Address</label> <input type="email" name="email" class="form-control" value="{{ old('email')}}"> <small class="text-danger">{{ $errors->first('email') }}</small> </div> <div class="form-group{{ $errors->has('message') ? ' has-error' : '' }}"> <label for="message">Message</label> <textarea name="message" class="form-control" rows="5">{{ old('message')}}</textarea> <small class="text-danger">{{ $errors->first('message') }}</small> </div> <div class="form-group"> <button type="submit" class="btn btn-primary">Submit</button> </div> </form> </div> </div> </div> </body> </html>

Above is a simple contact form with name , email and message fields.

Back to the ContactsController , add the code below to it:

// app/Http/Controllers/ContactsController.php /** * Handle the actual contacting * * @param Request $request * @return Response */ public function contact(Request $request) { $rules = [ 'name' => 'required', 'email' => 'required|email', 'message' => 'required|min:5', ]; $this->validate($request, $rules); // send contact details to email address return back()->with("status", "Your message has been received, We'll get back to you shortly."); }

The contact() handles the actual contacting. First, we create an array of validation rules then we pass it to the validate() alongside the form request data. What the validate() does is, validates the form request data against the validation rules set. If everything checks out, execution continues from the next line of code else validation errors are thrown with the appropriate error messages.

Before doing any kind of custom error messages, let's see how the default Laravel validation error messages look like. Let's start our demo contact form:

php artisan serve

and visit http://localhost:8000/contact to see the application in action.


Deep Dive Into Custom Validation Error Messages In Laravel
How Laravel Validation Error Messages Work

Like I said earlier, the default validation error messages should be fine for most Laravel applications (well, depending of the type of applications). What Laravel does by default is take the name attribute of each of the form input elements and use them to create error messages depending on the validation rules.

As you can see from the image above, Laravel took the name attributes (in our case, name, email and message) of each input elements of the contact form to create the validation error messages.

But how might we customize Laravel to display our custom validation messages? Well, that is what this tutorial is all about, which I will show you shortly.

Laravel has made customizing its defaults seamless as possible. I will show you two ways in which you can customize validation errror messages in your Laravel applications.

Using Language File

The simplest and straightforward way to customize your validation error messages is through the language file. Laravel by default ships with it, language (English) files. These files can be found in resources/lang/en directory. For this tutorial, we are only concerned with validation.php file.

Going through the language validation file, you can see that there are error messages for each of the validation rules provided by Laravel. Let's take a look at the defaut error message for the required validation rule:

'required' => 'The :attribute field is required.'

As you can see there is an :attribute placeholder that will be substituted with the name attribute of form input elements.

With this, we can easily customize the error message for the required validation rule.

// resources/lang/en/validation.php 'required' => 'The :attribute field can not be blank.'

If we try submiting the contact form now without filling the required fields, you should see our custom validation error message is now taking effect.


Deep Dive Into Custom Validation Error Messages In Laravel

But what if I want much more custom messages like We need your email address also. .

We can also specify custom validation messages for attributes:

// resources/lang/en/validation.php 'custom' => [ 'email' => [ 'required' => 'We need your email address also.', ], ]

The error message from the snippet above will only be applied to the email field as shown in the screenshot below.


Deep Dive Into Custom Validation Error Messages In Laravel

If you scroll down to the very bottom the the validation.php language file, you something like:

/* |-------------------------------------------------------------------------- | Custom Validation Attributes |-------------------------------------------------------------------------- | | The following language lines are used to swap attribute place-holders | with something more reader friendly such as E-Mail Address instead | of "email". This simply helps us make messages a little cleaner. | */ 'attributes' => []

Let's say we want the name attribute to be display as Full Name instead of name , we simply do:

// resources/lang/en/validation.php 'attributes' => [ 'name' => 'Full Name' ]

This will swap the name :attribute placeholder with Full Name.


Deep Dive Into Custom Validation Error Messages In Laravel

All the above works irrespective of whether you are using Form Requests, validate method or even the Validator Facade.

Directly In Code

If what whatsoever reasons you prefer not to use the language file for custom error messages. Another way to implement custom validation error messages in your Laravel applications is to add your custom error messages directly within your codes.

There are two approaches to using this method for custom error messages depending on how you implement validation in your application (that is, whether through Form Requests or validate method/Validator Facade).

First, let's take a look at how we would tackle custom error messages when you are using either validate() or Validator::make() . Both methods accepts four arguments (form request data, validation rules, messages, and custom attributes).

With this, we can easily add custom error messages to our application by passing a third argument to the validate() or Validator:make() containing an array of our custom error messages. Update the ContactsController 's contact() with the code below:

// app/Http/Controllers/ContactsController.php /** * Handle the actual contacting * * @param Request $request * @return Response */ public function contact(Request $request) { $rules = [ 'name' => 'required', 'email' => 'required|email', 'message' => 'required|min:5', ]; $customMessages = [ 'required' => 'The :attribute field can not be blank.' ]; $this->validate($request, $rules, $customMessages); // send contact details to email address return back()->with("status", "Your message has been received, We'll get back to you shortly."); }

Your custom error messages should now take effect when you try submitting the form without filling the required fields.


Deep Dive Into Custom Validation Error Messages In Laravel

We can also specify custom validation messages for specific attributes just as we saw with using language file. Update $customMessages array with the snippet below:

// app/Http/Controllers/ContactsController.php $customMessages = [ 'name.required' => 'Yo, what should I call you?', 'email.required' => 'We need your email address also', 'message.required' => 'c\'mon, you want to contact me without saying anything?', ];
Deep Dive Into Custom Validation Error Messages In Laravel

The approach with Form Requests is slightly different. Let's create a Form Request to demonstrate this.

php artisan make:request ContactFormRequest

Let's add our validation rules, paste the code below to the newly created ContactFormRequest file:

// app/Http/Requests/ContactFormRequest.php /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'name' => 'required', 'email' => 'required|email', 'message' => 'required|min:5', ]; }

We created our validation rules within the rules() just as we did in ContactsController controller.

Now let's add our custom validation messages:

// app/Http/Requests/ContactFormRequest.php /** * Get the error messages for the defined validation rules. * * @return array */ public function messages() { return [ 'name.required' => 'Yo, what should I call you?', 'email.required' => 'We need your email address also', 'message.required' => 'c\'mon, you want to contact me without saying anything?', ]; }

To implement custom error message when using Form Request, we simply add a messages() to ContactFormRequest . The message() simply returns an array containing our custom validation messages. In our case above, we set custom messages on the required validation rule of each of our input fields (name, email and message).

You should get the same result as we saw above.

Conclusion

Amazing right? Yeah its amazing. You can see our we have been able to customize our validation error messages without breaking a sweat. I hope you found this tutorial helpful. If you encounter any problem while following along with this tutorial, let's me know through the comment section below.

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: LaravelUTRY
分页:12
转载请注明
本文标题:Deep Dive Into Custom Validation Error Messages In Laravel
本站链接:http://www.codesec.net/view/520502.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(56)