In any project validation plays an important role.In laravel 4.x we have validation class which is used to
validating data and validating error message.
Basic Validation Example:
$validator = Validator::make(
array('name' => 'Dayle'),
array('name' => 'required|min:5')
);
Here we can see that make passes as first argument and second argument is validation rules which we
want to reply on our data.
If we want to apply multiple rule on the field then we will use array to implement this.
$validator = Validator::make(
array('name' => 'Dayle'),
array('name' => array('required', 'min:5'))
)
If we want to updating multiple field then
$validator = Validator::make(
array(
'name' => 'Dayle',
'password' => 'lamepassword',
'email' => 'email@example.com'
),
array(
'name' => 'required',
'password' => 'required|min:8',
'email' => 'required|email|unique:users'
)
);
When Validator instance is created then by using pass and fail method we will use to validate the data.
Example:
if ($validator->fails())
{
// The given data did not pass validation
}
0 Comment(s)