Laravel validation
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts
What is Laravel Validation?
- Validation is the most important featuers of designing an application. It validates the incoming data.
- Laravel provides several different approaches to validate your application's incoming data.
- By default, base controller class uses a ValidatesRequests trait which provides a convenient way to validate incoming HTTP requests with a variety of influential validation rules
Available Validation Rules in Laravel
Accepted | Active URL | After (Date) |
Alpha | Alpha Dash | Alpha Numeric |
Array | Before (Date) | Between |
Boolean | Confirmed | Date |
Date Format | Different | Digits |
Digits Between | Exists (Database) | |
Image (File) | In | Integer |
IP Address | JSON | Max |
MIME Types(File) | Min | Not In |
Numeric | Regular Expression | Required |
Required If | Required Unless | Required With |
Required With All | Required Without | Required Without All |
Same | Size | String |
Timezone | Unique (Database) | URL |
- Laravel will always check for errors in the session data, and automatically bind them to the view if they are accessible.
- So, it is important to note that a $errors variable will always be available in all of your views on every request, allowing you to conveniently assume the $errors variable is always defined and can be safely used.
- The $errors variable will be an instance of Illuminate\Support\MessageBag. Error message can be displayed in view file by adding the code as shown below.
Example
Step 1
- Create a controller called Validation Controller by executing the following command.
Step 2
- After successful execution, you will receive the following output
Step 3
- Copy the following code in app/Http/Controllers/ValidationController.php file.
app/Http/Controllers/ValidationController.php
Step 4
- Create a view file called resources/views/login.blade.php and copy the following code in that file
resources/views/login.blade.php
Step 5
- Add the following lines in app/Http/routes.php.
app/Http/routes.php
Step 6
- Visit the following URL to test the validation.
Step 7
- Click the “Login” button without entering anything in the text field. The output will be as shown in the following image.