定制 inani/phpvalidate 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

inani/phpvalidate

Composer 安装命令:

composer require inani/phpvalidate

包简介

The lightest way to validate your inputs.

README 文档

README

You have some input data to validate but you don't want to grab the huge libraries to validate them? Well here is the solution, PHPValidate is a very light libraries that allows you to validate simple data and enables you to create custom constraint at any time! Come on give it a try!

Installation

composer require inani/phpvalidate 

Usage

Feed the Validator with the array data to verifies

$validator = new Validator([  'name' => 'Inani El Houssain']);

Build your Rules

// Only if required and without length greater or equal than 10 characters
$validator->addRule(( new Rule('name'))->required()->min(10));

Check the rules

$bool = $validator->check();

Get the errors list

// get the errors of the name field, all the fields if not specified
$validator->getErrors('name');

Available rules

rule README
required Field not empty
min The minimum characters
max The maximum number of characters
number the input should be a numeric value

Custom Constraints

Every custom rule should extends the CustomConstraint Class and implements the two methods, for instance creating a rule that validates an email field:

<?php
use Inani\PHPValidate\CustomConstraint;

class ShouldBeAnEmail extends CustomConstraint
{
  /**
   * Check if the field is Required
   *
   * @param null $value
   * @return bool
   */
  public function isValid($value = null)
  {
    if(!filter_var($value, FILTER_VALIDATE_EMAIL)){

      $this->errorFound();

      return false;
    }

    return true;
  }

  public function getError()
  {
    return "This is not a valid email";
  }
}

And its usage is as simple as injecting the Rule in the rules array like this :

$validator->addRule(( new Rule('email'))->required()->inject(new ShouldBeAnEmail));

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固