Originally from: HTTPS://LARAVIST.COM/ARTICLE/15
Laravist is my just on-line laravel community, there are any issues related to Laravel can come here to ask me, I will try to help you solve the problem, later will try to record some video tutorials, the form is probably like this
Https://laravist.com/lesson/1
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 on the first one, but also recommend that you use the first form validation:
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
Using the Artisan make:request command in the project directory, you can generate a request class for form validation, which we name here as Storearticlerequest, and you can name it in the way you like it, 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 later look at the code when there are many advantages. The file generated by this command is located in the app/http/requests/folder, and we open this file to see:
Class Storearticlerequest 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. * * @return Array */public function rules () { return [ // ]; }}
Implementing validation
You can see that there will be two methods: authorize () and rules (). Authorize () can easily 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, B can be edited. If not, the return is false and the modification returns true if possible. 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 ' ]; }
Because when we create the article, we have two submitted fields: Title and content. This is the two fields we set when designing the articles table.
Then, the validation rule above is: for the title and content two fields, we need the user to fill in the content, 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 simply, we only need to modify the Articlecontroller store () method slightly:
Public function Store (requests\storearticlerequest $request) { $input = $request->all (); Add two lines below, and by the way see Request::get's use $input [' intro '] = Mb_substr ($request->get (' content '), 0,64); }
We pass an instance of the entire Storearticlerequest class to the store () method with the $request variable, and this time, laravel
will automatically check whether we need to do form validation (the rules method has no validation rules defined), if there is a need to verify, Laravel will first go to verify this road, if the validation is not passed, the code in the store () method will not be executed, but directly to the submission form of the page, Here is: http://blog.dev/article/create this page. If all the validations are passed, the code inside the store () will be executed only if it is executed to $input = $request->all (); Here and down the code ... For example, what happens when we try to leave a blank?
Feedback Error
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 ())
@foreach ($errors->all () as $error)
- {{$error}}
@endforeach
@endif
In create.blade.php this view file to add the above code, I here is put in {!! Form::close ()!!} Behind. 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 change it to resources/lang/en/validation.php, or 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 title of the article to be at least three bytes long? We can do this, in the Rules () method:
' Title ' = ' Required|min:3 ',
In Laravel, we use | To separate multiple rules: represents the value 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 an 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, e-Mail on behalf of the verification of the information is a correct format of the mailbox, as for the confirmation password to use confirmed to specify, here note is confirmed instead of confirme. and the second input password field needs to be written in the form of password_confirmation, that is, in the view, we will have a similar input form:
For more validation rules, refer to the official documentation:
Http://laravel.com/docs/5.1/validation
Using validation
You can remove the variables from the store (Requests\storearticlerequest $request) before using this
When using validation, it is useful to verify some simple forms validation. This demo is written directly in Articlecontroller, using Validator::make () directly, using Validator::make (array $request, array $rules), For example, our examples can be written in the store ():
$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.
Summarize
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