In Laravel, how to verify that the value of an input field is not empty?

WBOY
Release: 2023-09-07 20:50:02
forward
1498 people have browsed it

In Laravel, how to verify that the value of an input field is not empty?

To validate data, you can use the Validation class. Verification helps to verify data and display error messages to the user.

Example 1

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')
);
Copy after login

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.

Example 2

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";
      }
   }
}
Copy after login

Output

The above output is -

Validation Successful
Copy after login

Example 3

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";
      }
   }
}
Copy after login

Output

The output of the above code is -

Validation Failed
Copy after login
Copy after login

Example 4

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";
      }
   }
}
Copy after login

Output

The output of the above code is -

Validation Failed
Copy after login
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!