Validating user input in laravel

In most web applications, there will be certain form fields that are required to process the
form. We also want to be sure that all the e-mail addresses are formatted correctly, or the
input must have a certain number of characters. Using Laravel’s Validator class, we can
check for these rules and let the user know if something is not correct.

Getting ready

For this recipe, we just need a standard installation of Laravel.

How to do it…

To complete this recipe, follow these steps:
1. Create a route to hold the form:
Route::get(‘userform’, function()
{
return View::make(‘userform’);
});
2. Create a view named userform.php and add a form:
<h1>User Info</h1>
<?php $messages = $errors->all(‘<p
style=”color:red”>:message</p>’) ?>
<?php
foreach ($messages as $msg)
{
echo $msg;
}
?>
<?= Form::open() ?>
<?= Form::label(’email’, ‘Email’) ?>
<?= Form::text(’email’, Input::old(’email’)) ?>
<br>
<?= Form::label(‘username’, ‘Username’) ?>
<?= Form::text(‘username’, Input::old(‘username’)) ?>
<br>
<?= Form::label(‘password’, ‘Password’) ?>
<?= Form::password(‘password’) ?>
<br>
<?= Form::label(‘password_confirm’, ‘Retype your Password’)
?>

<?= Form::password(‘password_confirm’) ?>
<br>
<?= Form::label(‘color’, ‘Favorite Color’) ?>
<?= Form::select(‘color’, array(‘red’ => ‘red’, ‘green’ =>
‘green’, ‘blue’ => ‘blue’), Input::old(‘color’)) ?>
<br>
<?= Form::submit(‘Send it!’) ?>
<?php echo Form::close() ?>
3. Create a route that handles our POST data and validates it:
Route::post(‘userform’, function()
{
$rules = array(
’email’ => ‘required|email|different:username’,
‘username’ => ‘required|min:6’,
‘password’ => ‘required|same:password_confirm’
);
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails())
{
return Redirect::to(‘userform’)-
>withErrors($validation)->withInput();
}
return Redirect::to(‘userresults’)->withInput();
});

4. Create a route to handle a successful form submission:
Route::get(‘userresults’, function()
{
return dd(Input::old());
});

How it works…

In our form page, we begin by checking if there are any errors and displaying them if found.
Inside the error, we can set the default style for each error message. We also have the option
of checking for and displaying errors for individual fields using $errors->get(’email’).
The $errors variable is automatically created by Laravel if it detects a flashed error.
Next, we create our form. In the last parameter of the form elements, we’re getting
Input::old(), which we use to store the previous input if the validation happens to fail.
That way, the user won’t need to keep filling out the entire form.

We then create a route where the form is POSTed, and set up our validation rules. In this case,
we use the required rule for email, username, and password, to make sure something is
typed into those fields.
The email field also gets the email rule, which uses PHP’s built-in FILTER_VALIDATE_EMAIL
filter of the filter_var function. The email field must also not be the same as the username
field. The username field uses the size validation to check for at least six characters. Then the
password field checks the value of the password_confirm field and makes sure they’re
the same.
Then, we create the validator and pass in all of the form data. If any of those rules aren’t met,
we navigate the user back to the form, and also send back any validation error messages as
well as the original form input.
If the validation passes, we go to the next page using Laravel’s dd() helper function, which
uses var_dump() to show the form values on the page.

Leave a Reply

Your email address will not be published. Required fields are marked *