承接 abdelrhmansaid/validator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

abdelrhmansaid/validator

Composer 安装命令:

composer require abdelrhmansaid/validator

包简介

Validation framework lets you configure, rather than code, your validation logic.

README 文档

README

tests

Validation library lets you configure, rather than code, your validation logic.

Installation

composer require abdelrhmansaid/validator

Testing

composer test

Usage

After registering the rules that you want to use, you can use the validator like this:

use AbdelrhmanSaid\Validator\Validator;

/* Instantiate a new validator */
$validator = new Validator($email);

/* Or you can use the static method init */
$validator = Validator::init($email);

/* Apply your rules */
$validator->email()->required()->max(255);

if (!$validator->validate()) {
    return $validator; // validation result in JSON format
}

Also, you can validate multiple values at once:

$errors = Validator::initMultiple($_POST, [
    'email' => 'email',
    'password' => 'required|min:6|max:255'
]);

if (count($errors)) {
    // do something
}

Note that multiple validations return an array of failures rather than a Validator instance.

Btw, you can validate values statically:

$isEmail = Validator::email('admin@example.com'); // true

Registering rules

The validator came without any registered rules by default. You can add them by using the Validator::addRule() method.

use AbdelrhmanSaid\Validator\Rules\RequiredRule;

Validator::addRule(RequiredRule::class);

Also you can load the default rules by using the Validator::loadDefaultRules() method.

Validator::loadDefaultRules();

Loading the default rules will register the following rules:

Rule Description Parameters
alpha The value must contain only alphabetic characters. -
between The value must be between the given min and max. min: int, max: int
contains The value must contain all the given values. mixed[]
doesntContain The value must not contain all the given values. mixed[]
each The value must be an array and each item must pass the given rule. callable
email The value must be a valid email address. -
equal The value must be equal to the given value. mixed
date The value must be a valid date. -
max The value must be less than or equal to the given value. int
min The value must be greater than or equal to the given value. int
pattern The value must match the given pattern. string
required The value must be present. -
string The value must be a string. -
number The value must be a number. -
array The value must be an array. -

You can submit a pull request to add a new rule.

Custom rules

If you have a specific rule you want to use, you can create a class that extends Validator\AbstractRule and register it.

class CustomRule extends AbstractRule
{
    protected string $message = '...';

    public function getName(): string
    {
        // name will be used to call the rule
    }

    public function validate(mixed $value, mixed ...$params): bool
    {
        // validation logic
    }
}

Custom messages

If you want to customize the error messages, you can use the Validator::setMessages() method.

Validator::setMessages([
    'required' => 'The value is required.',
    'email' => 'The value is not a valid email.',
    'max' => 'The value should be less than or equal to {0}.',
]);

Note that you can pass parameters to the message using {x} placeholders where x is the index of the parameter.

That's it. Enjoy 👌!

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固