satanvir/validator

A PHP package that allows you to handling form request validation.

dev-main 2023-05-09 11:16 UTC

This package is auto-updated.

Last update: 2024-09-09 14:29:31 UTC


README

A PHP package that allows you to handling form request validation.

Requirements

  • PHP "^8.0"

Installation

You can install the package via composer:

composer require satanvir/validator

Usage

Here's an example of how to use this Validator package for validating input reques:

use Satanvir\Validator\Validator;

$validator = new Validator($config); // Validator(array $config = [])

Passing User's Inputs

$validator->request($_POST); // Validator::request(array $inputs): self 

Define Rules

You can define rules by two ways.

  1. Bulk way
  2. Single Input

Bulk way

$rules = [
'name' => 'required|min:3|max:60',
'email' => ['required', 'email']
];

$validator->rules($rules); // Validator::rules(array $rules): self
 

Single Input

$validator->rule('email', 'required|email'); // Validator::rule(string $name, array|string $rule): self

 

Validation

$validator->validate(); // Validator::validate(): self
 

*** This method validate the given inputs by using the given rules ***

Check: Validation has failed

if ($validator->fails()) {
// do something
}
// Validator::fails(): bool
 

*** Return true if validation failed ***

Check: Validation has passed

if ($validator->passed()) {
// do something
}
// Validator::passed(): bool
 

*** Return true if validation passed ***

Getting Errors

Getting all errors

foreach ($validator->errors() as $error) {
// do something
}
// Validator::errors(): array

 

*** errors() method returns all generated errors from validators ***

Getting all errors of a single input

foreach ($validator->error('email') as $error) {
// do something
}
// Validator::error(string $name): array
 

Getting first error

echo $validator->errorFirst();
//or
echo $validator->errorFirst('email');
// Validator:errorFirst(?string $name = null): ?string

*** if did not pass any param, then return first error from all otherwise return from specific input. ***