Giter Club home page Giter Club logo

le7-validator's Introduction

le7-validator

This is a simple and convenient PHP validator. It can be used to validate form input, API requests, and any other data that needs to be validated before being processed.

Requirements

  • PHP 8.1

Installation

composer require rnr1721/le7-validator

Testing

composer test

Usage

First you need to create an instance of the validator:

use Core\Utils\ValidatorFactory;

$validatorFactory = new ValidatorFactory();
$validator = $validatorFactory->getValidator();

Then you need to define validation rules for each field:

Method setFullRule can take three arguments:

  • Field - Field (key) for validate.
  • Value - Data to validate
  • Rules - Validation rules
  • Label - Human-like name of field. It uses for get errors
$validator->setFullRule('login', 'john', 'required|minlength:3|maxlength:5', 'Login');
$validator->setFullRule('email', '[email protected]', 'required|email', 'User email');
$validator->setFullRule('age', 35, 'min:22|max:55', 'User age');

And finally, you can check:

if ($validator->validate()) {
    // Validation success
} else {
    // Validation not success
    $errors = $validator->getMessages();
}

Present rules

Rules - is string of "|" - separated rules. For example:

minlength:3|maxlength:10|numeric
  • required: The field must be filled
  • min:{n}: Field value must be at least {n}
  • max:{n}: Field value must be no more than {n}
  • minlength:{n}: The length of the string field value must be at least {n}
  • maxlength:{n}: The length of the string field value must be no more than {n}
  • email: The field value must be a valid email address
  • notempty: The field value must not be empty or contain only spaces
  • numeric: Validates that only numeric data
  • email_dns: validates the format of an email address and checks if the domain part of the email address has a valid DNS record
  • url: Validate URL
  • url_active: If URL address is valid and exists
  • date: Validates that value is date
  • date_format:{n}: Validates date format: Example: date_format:Y-m-d
  • date_before:{n}: Validate date before some date. Example: date_before:2022-05-15
  • date_after:{n}: Validate date after some date. Example: date_after:2022-05-15
  • boolean: Validate boolean

le7-validator's People

Contributors

rnr1721 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.