Laravel Validation

No comments

Validation is one of the fundamental elements of any application. Laravel Framework provides several approaches to validate incoming user data. ValidatesRequests Trait is used by Laravel’s Base Controller to validate incoming HTTP requests. It also supports a variety of powerful rules.

# Adding Validations

To add validation logic to check user data, we use the validate method provided by the Illuminate\Http\Request object. This is because we can fetch user input data with the Request class.
The code continues execution normally if the validation rules pass. But if the validation fails, an exception is thrown and the user is informed with an automatic error message. For a better perspective, let us write some validation rules:

# controller

   public function store(Request $request)
{
$request->validate([
'title' => 'required|unique:posts|max:255',
'category' => 'required',
]);
}

# Validating Nested Parameters

In case HTTP layout or request comprise of nested parameters, we can specify them with a “.“(dot) operator in the validation rules like:

# controller
public function store(Request $request)
{
$request->validate([
'title' => 'required|max:255',
'author.name' => 'required',
'author.contact' => 'required',
]);
}

# Displaying Validation Errors

The next step is to determine a way to handle exceptions thrown on validation failures. As we discussed earlier, Laravel automatically sends an error message. It also redirects the user to the previous location where the error is supposed to be rectified. Additionally, Laravel also flashes these messages on sessions so that we can have global access to them amidst controllers and layout views.

This eliminates the need to explicitly bind the error messages to views or routes. Laravel keeps a check on session error data and automatically binds them to views if available.

Let us see how we can utilize these session error messages to display to our users through views:

# blade

 

@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

we have a new validation option: image dimensions for image uploads. The validation rule is called dimensions, and you can pass the following parameters to it:

min_width: Images narrower than this pixel width will be rejected
max_width: Images wider than this pixel width will be rejected
min_height: Images shorter than this pixel height will be rejected
max_height: Images taller than this pixel height will be rejected
width: Images not exactly this pixel width will be rejected
height: Images not exactly this pixel height will be rejected
ratio: Images not exactly this ratio (width/height, expressed as “width/height”) will be rejected

You can combine any rules that make sense together. Let’s take a look at a few examples. First, let’s set up our base install.

// routes file
Route::get('/', function () {
return view('form');
});


Route::post('/', 'ImageController@postImage');

//form.blade.php

<form method="POST" enctype="multipart/form-data">
<input type="file" name="avatar">
<input type="submit">
</form>

Now, let’s make our ImageController and take a look at a few sample validations.

// ImageController
public function postImage(Request $request)
{
$this->validate($request, [
'avatar' => 'dimensions:min_width=250,min_height=500'
]);

// or…

$this->validate($request, [
'avatar' => 'dimensions:min_width=500,max_width=1500'
]);

// or…

$this->validate($request, [
'avatar' => 'dimensions:width=100,height=100'
]);

// or…

// Ensures that the width of the image is 1.5x the height
$this->validate($request, [
'avatar' => 'dimensions:ratio=3/2'
]);
}

SatishLaravel Validation

Leave a Reply

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