
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Validate Input Field for Non-Null Value in Laravel
To validate data you can make use of the Validation class. The validation helps to validate data as well as to display error messages to the user.
Example 1
In the example below the make() method is used. The first argument is the data to be validated and the second is the rule applied on the data : name.
$validator = Validator::make( array('name' => 'Disha'), array('name' => 'required|min:5') );
As per the above the name assigned is Disha. As per the rule the name is mandatory and the minimum characters required is 5.
Example 2
In the below example we have taken formdata with firstname, lastname and address. The required rule is applied on all the three input fields. If any of them is not given the validation will fail. Similarly you can also set the minimum characters required.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; use Illuminate\Routing\Router; use Illuminate\Validation\Rule; class testuserip extends Controller { public function index() { $formData = array( 'firstname' => 'Siya', 'lastname' => 'Nadkarni', 'address' => 'xyz' ); $rules['firstname'] = 'required|string'; $rules['lastname'] = 'required|string'; $rules['address'] = 'required|string'; // validate $validator = Validator::make($formData, $rules); if ($validator->fails()) { echo "Validation Failed"; } else { echo "Validation Successful"; } } }
Output
The output for above is ?
Validation Successful
Example 3
In the example below I define a rule on the input field as required and the field is not passed. Will see the validation failed message being shown in the output.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; use Illuminate\Routing\Router; use Illuminate\Validation\Rule; class testuserip extends Controller { public function index() { $formData = array( 'lastname' => 'Nadkarni', 'address' => 'xyz' ); $rules['firstname'] = 'required|string'; $rules['lastname'] = 'required|string'; $rules['address'] = 'required|string'; // validate $validator = Validator::make($formData, $rules); if ($validator->fails()) { echo "Validation Failed"; } else { echo "Validation Successful"; } } }
Output
The output of the above code is ?
Validation Failed
Example 4
In the following example will pass a null value to the input field and see the validation status ?
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; use Illuminate\Routing\Router; use Illuminate\Validation\Rule; class testuserip extends Controller { public function index() { $formData = array( 'firstname' =>null, 'lastname' => 'Nadkarni', 'address' => 'xyz' ); $rules['firstname'] = 'required|string'; $rules['lastname'] = 'required|string'; $rules['address'] = 'required|string'; // validate $validator = Validator::make($formData, $rules); if ($validator->fails()) { echo "Validation Failed"; } else { echo "Validation Successful"; } } }
Output
The output of the above code is ?
Validation Failed
It gives validation failed message since firstname is a required field and cannot have null values.