- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to validate aninput field if the value is not NULL 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.
- Related Articles
- MongoDB query to select one field if the other is null and the first field if both are not null?
- How to validate an array in Laravel?
- How to validate Route Parameters in Laravel?
- How to validate exact words in Laravel?
- How do you check if a field is NOT NULL with Eloquent?
- Insert default into not null column if value is null in MySQL?
- How to update a field with a particular value if it is null in MySQL?
- How to check if field is null or empty in MySQL?
- How to query for records where field is null or not set in MongoDB?
- MongoDB query to select one field if the other is null?
- What is MySQL NOT NULL constraint and how can we declare a field NOT NULL while creating a table?
- Check if a field of table has NOT NULL property set in SQL?
- Enum with NOT NULL in a MySQL field?
- How to make Laravel (Blade) text field read-only?
- Display the result with not null value first and then with null value in MySQL
