To validate data, you can use the Validation class. Verification helps to verify data and display error messages to the user.
In the following example, the make() method is used. The first parameter is the data to be processed Verified, the second is the rule that applies to data:name.
$validator = Validator::make( array('name' => 'Disha'), array('name' => 'required|min:5') );
The name assigned as per the above is Disha. According to the rules, the name is mandatory and The minimum number of characters required is 5.
In the example below, we use form data containing first name, last name, and address. this The required rule applies to all three input fields. if any of them are not given Authentication will fail. Likewise, you can set the minimum number of 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"; } } }
The above output is -
Validation Successful
In the example below, I have defined the rule on the input field as required, but the field is not passing. You will see a validation failure message displayed 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"; } } }
The output of the above code is -
Validation Failed
In the following example, a null value will be passed to the input field and checked for validation state -
<?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"; } } }
The output of the above code is -
Validation Failed
It gives the message that validation failed because name is a required field and cannot be empty value.
The above is the detailed content of In Laravel, how to verify that the value of an input field is not empty?. For more information, please follow other related articles on the PHP Chinese website!