Laravel Classroom image

Prashant  Nigam / Student / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9|Last
Lessons:-Handling Input

8 Validation
Validation is an important part of many web applications. You can never trust your users, they
have been plotting to destroy you for weeks by abusing your forms with evil javascripts.
We can’t let them win, they must not destroy our beautiful applications. Let’s validate all input
provided by the user, that way they won’t be able to harm us at all.
Naturally Laravel has a library, aptly named ‘Validation’ that will do all the hard work for us.
8.1 Set up validation
Let’s start by creating an imaginary form, close your eyes and imagine a nice long form with
many fields… uh oh… how can I get you to open your eyes again..?
Right, I will assume you got fed up of waiting, have opened your eyes and are back with me
again, along with our imaginary form. Let’s get the input data from that form.
1 <?php
2
3 $input = Input::get();
Now normally you don’t want to use the get() method, as its an easy way to populate your
input array with extra data you don’t need. In fact the open source collaboration site github was
a victim to mass assignment. I have used get() to simplify the tutorial. In your applications
please build the input array only with the fields you need.
Our input array now contains something that looks a little like this..
1 <?php
2
3 array(
4 'name' => 'John',
5 'age' => 15
6 )
Let’s validate these fields to make sure they make sense to our application. Before we can start
the validation process we need to create a set of rules that will be used to validate each field.
With the validator class, rules are defined in an array format. Let’s jump right in and take a look.
1 <?php
2
3 $rules = array(
4 'name' => 'required|min:3|max:32|alpha',
5 'age' => 'required|integer|min:16'
6 );8 Validation
Validation is an important part of many web applications. You can never trust your users, they
have been plotting to destroy you for weeks by abusing your forms with evil javascripts.
We can’t let them win, they must not destroy our beautiful applications. Let’s validate all input
provided by the user, that way they won’t be able to harm us at all.
Naturally Laravel has a library, aptly named ‘Validation’ that will do all the hard work for us.
8.1 Set up validation
Let’s start by creating an imaginary form, close your eyes and imagine a nice long form with
many fields… uh oh… how can I get you to open your eyes again..?
Right, I will assume you got fed up of waiting, have opened your eyes and are back with me
again, along with our imaginary form. Let’s get the input data from that form.
1 <?php
2
3 $input = Input::get();
Now normally you don’t want to use the get() method, as its an easy way to populate your
input array with extra data you don’t need. In fact the open source collaboration site github was
a victim to mass assignment. I have used get() to simplify the tutorial. In your applications
please build the input array only with the fields you need.
Our input array now contains something that looks a little like this..
1 <?php
2
3 array(
4 'name' => 'John',
5 'age' => 15
6 )
Let’s validate these fields to make sure they make sense to our application. Before we can start
the validation process we need to create a set of rules that will be used to validate each field.
With the validator class, rules are defined in an array format. Let’s jump right in and take a look.
1 <?php
2
3 $rules = array(
4 'name' => 'required|min:3|max:32|alpha',
5 'age' => 'required|integer|min:16'
6 );8 Validation
Validation is an important part of many web applications. You can never trust your users, they
have been plotting to destroy you for weeks by abusing your forms with evil javascripts.
We can’t let them win, they must not destroy our beautiful applications. Let’s validate all input
provided by the user, that way they won’t be able to harm us at all.
Naturally Laravel has a library, aptly named ‘Validation’ that will do all the hard work for us.
8.1 Set up validation
Let’s start by creating an imaginary form, close your eyes and imagine a nice long form with
many fields… uh oh… how can I get you to open your eyes again..?
Right, I will assume you got fed up of waiting, have opened your eyes and are back with me
again, along with our imaginary form. Let’s get the input data from that form.
1 <?php
2
3 $input = Input::get();
Now normally you don’t want to use the get() method, as its an easy way to populate your
input array with extra data you don’t need. In fact the open source collaboration site github was
a victim to mass assignment. I have used get() to simplify the tutorial. In your applications
please build the input array only with the fields you need.
Our input array now contains something that looks a little like this..
1 <?php
2
3 array(
4 'name' => 'John',
5 'age' => 15
6 )
Let’s validate these fields to make sure they make sense to our application. Before we can start
the validation process we need to create a set of rules that will be used to validate each field.
With the validator class, rules are defined in an array format. Let’s jump right in and take a look.
1 <?php
2
3 $rules = array(
4 'name' => 'required|min:3|max:32|alpha',
5 'age' => 'required|integer|min:16'
6 );Great, now we have some rules. The array key is the field that is being validated upon, and the
array value contains a number of validation rules separate by a pipe | symbol.
In our case we are validating that both fields contain a value by using the ‘required’ rule. The
length of the user’s name must be a minimum of 3 characters (min:3) and a maximum length of
32 characters (max:32). The ‘alpha’ rule will check to make sure that the name field only contains
letters.
Our age field must contain an integer and the value must be at least 16. You see that the min
rule has adapted to fit the content that its validating, very clever!
Don’t worry, we will cover all the validation rules later. For now let’s see the validation in action,
here we go.
1 <?php
2
3 $v = Validator::make($input, $rules);
We have created our validator object with the make() method, passing it our input array and our
rules array. Let’s see if it validates!
1 <?php
2
3 if( $v->fails() )
4 {
5 // code for validation failure :(
6 }
7 else
8 {
9 // code for validation success!
10 }
As you can see, we use the fails() method to check the result of the validation attempt, it will
return true if the validation has failed and false if it was successful.
If you prefer a more positive outlook on your validations, you could use the passes() method,
which returns the opposite values..
1 <?php
2
3 if( $v->passes() )
4 {
5 // code for validation success!
6 }
7 else
8 {
9 // code for validation failure :(
10 }
There, now we are positive and can dance over rainbows with sparkleponies.

 

 
 
 

Prashant  Nigam

Skills    Laravel

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)