Laravel Tutorial VII: Form Validation Validation
This article is original article, without consent, prohibit reprint.
Finally to update the seventh of this Laravel series tutorial, during the period to write a little something else.
Do not say nonsense, directly into the form validation part of it. There are forms in almost every Web application, and forms are essential to form validation. In Laravel, it can be said that there are two ways to form validation: Using request and using validation. The following sections will be divided into two parts, and I will focus more on the first, but also recommend that you use the first
Request Form Validation
Why would you focus on the first way of proving? Because individuals feel that the first way is easier to maintain and code re-usable under the same validation conditions. And the form of code is more appropriate for laravel and my personal habits: You can use the command line to generate code. That is, you can use the Artisan tool:
php artisan make:request StoreArticleRequest
In the project directory using the Artisan make:request
command can be generated for the form validation Request
class, this class we named here StoreArticleRequest
, you can also be named in your own way, but I still recommend you in the name of the time to try to make the names more humane, This will be compared to the late look at the code when there are many advantages. The file generated by this command is located in app/Http/Requests/
This folder, and we open this file to see:
Classstorearticlerequest extends Request {/** * Determine if the user is authorized to make this request. * @return BOOL */public function authorize () {return FALSE; } /** * Get the validation rules that apply to the request. * @retu RN Array */public function rules () {return [ //]; }}
Implementing validation
You can see that there will be two ways: authorize()
and rules()
. authorize()
It is easy to understand whether we need to authenticate when we are dealing with this form request (usually a POST request), such as a comment published by A and B can be edited. If not, the return is preserved, and false
if so, the modification is returned true
. Then our logic here is: since it is published articles, in our site registered users (if open registration) can be published articles, so we first modify the authorize()
method, the return value of the change to: return true;
.
Then for the rules()
method, we need to set up our validation rules here, for example, we can set the following validation rules:
public function rules() { return [ ‘title‘ => ‘required‘, ‘content‘ => ‘required‘ ]; }
Since we are creating the article, we will have two submitted fields: title
and content
. This is the articles
two fields we set when we design the table.
Then, the validation rule above is: for title
and content
two fields, we need the user to populate it with content and cannot be empty.
Now that the rules are set up here, how do we apply them? So how do we validate the article before it is stored in the database? Very simple, we only need to modify ArticleController
the store()
method slightly:
public function store (requests\storearticlerequest $request) { $input = $request->all (); Span class= "hljs-comment" >//add two lines below, by the way request::get use $input [ ' intro '] = mb_substr ( $request->get ( 0,64);}
We pass an StoreArticleRequest
instance of the entire class into the method with a $request
variable, and at store()
this point, Laravel will automatically check if we need to validate the form (the rules method has no validation rules defined), and if there is a need to verify it, Laravel will go first to verify the path, If the validation does not pass, store()
the code inside the method is not executed, but instead jumps directly to the page where the form is submitted, which is: http://blog.dev/article/create this page. If all the validations are passed, the internal code will be executed, and the store()
code will be executed $input = $request->all()
here as well as down. For example, what happens when we try to leave a blank?
# #反馈错误
There seems to be no change in the picture above, but in fact it has been submitted once, but immediately jumps back. We can use the following method to verify:
@if($errors->any()) <ul class="alert alert-danger"> @foreach($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul>@endif
create.blade.php
Add the above code to this view file and I'll put it in the {!! Form::close() !!}
back. The idea here is that if there is any form validation error message, we will tell you that this information is printed out and fed back to the user. If not, the information is not displayed. Let's try it again:
At this point, we can see that the corresponding error message is displayed on the page when the form validation is not passed. If there are no errors, create an article.
Tips: If you don't want the error message to be in English, you can resources/lang/en/validation.php
change it, or you create a new language file package directly.
Multiple validation rules
OK, here we are basically going through this verification process. However, in the actual development, our validation is not all this simple: what if we want to set multiple validation rules for a field? For example, do we want the minimum number of articles to title
be less than three bytes long? We can do this, in the rules()
method:
‘title‘ => ‘required|min:3‘,
In Laravel, we use |
to separate multiple rules to :
represent the values of a rule. In fact, you can also use the array, but I still recommend you like the above to write, concise and clear.
Other common validation rules
As for more validation rules, such as when registering, verifying one email
and confirming the password, how do we need to write it?
‘email‘=>‘required|email‘,‘password‘=>‘required|min:6|confirmed‘,‘password_confirmation‘ => ‘required|min:6‘
Above I directly give these two common scenes of the most common wording, email
on behalf of the verification of the information is a correct format of the mailbox, as for the confirmation password is used confirmed
to specify, here attention is confirmed
not confirme
. And the second time you enter the password field needs to be written password_confirmation
in this form, that is, in the view, we will have a similar type of input form:
<input type="password" name="password" /><input type="password" name="password_confirmation" />
For more validation rules, refer to the official documentation:
Http://laravel.com/docs/5.1/validation
Using validation
Before using this, you can store(Requests\StoreArticleRequest $request)
remove the variables in
When using validation, it is useful to verify some simple forms validation. Here the demo is written directly in, directly, using, ArticleController
Validator::make()
for Validator::make(array $request,array $rules)
example, our example can be store()
written in:
$input = Request::all();$validator = Validator::make($input, [ ‘title‘ => ‘required|min:3‘, ‘body‘ => ‘required‘,]);
You can then use the following method to check that the validation has passed:
if ($validator->fails()) { }
After validation passes, we do the next steps, such as storing data in a database. A basic validation process is complete, and the part about validation, I just want to introduce here, because I will amway use the first way: Request.
Tips: The same authentication mechanism is used behind both approaches.
# #总结
Here is basically the basis of the form verification is finished, the next section I would like to talk about the use of Queryscope and SetAttributes, these two for our data warehousing and code reuse is very helpful, so next time will say the two knowledge points: We will first on the Published_ At the setting and use of this field, you will know the benefits of setting this field.
Last: Happy Hacking
Laravel Tutorial VII: Form Validation Validation