定制 xphoenyx/valify 二次开发

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

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

xphoenyx/valify

最新稳定版本:v1.11

Composer 安装命令:

composer require xphoenyx/valify

包简介

A little framework for user input validation

README 文档

README

NB! This project is archived thus not maintained anymore

Valify

A little framework for user input validation. It is still in development, so keep an eye on commits. Inspired by Yii2 input validation implementation.

Requirements

You need PHP 5.4 to run this code.

Installation

After downloading source, add next code to file, where you data is going to be validated:

require 'valify/Validator.php';
$validator = new Validator();

Framework uses namespaces, so add next line to the top of file, where validator is called:

use valify\Validator;

There is also a more straightforward way to install this framework through the compser. In your project root, issue next command in terminal:

php composer.phar require xphoenyx/valify 1.*

Now you are ready to validate your data.

Hint for a MVC pattern users

You can implement your own methods in base model class. Please investigate an example below:

use valify\Validator;

class Model {
    /* ... */
    
    protected $validator;
    // Your rules
    public $rules = [];
    // Here is stored $_POST, for example
    public $data = [];

    function __construct() {
        /* ... */
        $this->validator = new Validator();
        /* ... */
    }

    /*
     * Your other methods
     */

    public function validate() {
        return $this->validator
            ->setRules($this->rules)
            ->loadData($this->data)
            ->validate();
    }

    public function getErrors() {
        return $this->validator->getErrors();
    }
}

Usage

Usage is similar to Yii2 input validation.

Define rules

$rules = [
    [['username', 'password'], 'string', 'max'=>10],
    ['email', 'email', 'message'=>'Please provide a valid email'],
    ['remember_me', 'boolean']
    /* ... */
];

Each validator accepts message parameter, which should contain an error message as string. You can access attribute name and its value in message by using so-called 'patterns':

['email', 'email', 'message'=>'{value} for attribute "{attribute}" is not a valid email'],

NB! If the value is not representable as a string, value type will be shown instead of value itself

You can also implement your own validators by extending valify\validator\AbstractValidator class. In this case, if you are not using composer autoloader, you should also import (require) AbstractValidator. To use own validator in rules, just define validator namespace as a validator name:

$rules = [
    /* ... */
    ['email', '\\examples\\ExampleValidator', 'ownProperty'=>'abc' /* ... */]
    /* ... */
];

Make sure your validator is loaded before defining a namespace in rules. Refer to the valify\validators\ExampleValidator for detailed implementation info.

Define data to be validated

Input data is expected in next format:

$data = [
    'username'=>'username',
    'password'=>'123qwe',
    'email'=>'address@gmail.com',
    'remember_me'=>'1',
    /* ... */
];

Set rules and data

$validator = new Validator();
$validator = $validator->setRules($rules)->loadData($data)

You can call setrules() and loadData() multiple times:

$validator = new Validator();
$validator = $validator
                ->setRules([...])
                ->loadData([...])
                ->setRules([...])
                ->setRules([...])
                ->loadData([...])
                ->loadData([...]);

Execute validation

$isValid = $validator->validate();

You have an ability to perform a single value validation, without calling setRules() and loadData():

$password = $_POST['password'];
$isValid = Validator::validateFor('string', $password, ['min'=>6, 'max'=>20]);

For multiple value validation, pass an array of desired values as a second argument:

$values = [
    $_POST['username'],
    $_POST['first_name'],
    $_POST['password'],
];
$isValid = Validator::validateFor('string', $values, ['min'=>3, 'max'=>30]);

validateFor() will return an object with two properties:

  • isValid - contains boolean value;
  • lastError - contains last validation error message;
  • errors - contains whole error message stack for validating attribute;

Fetch error messages

if($validator->hasErrors()) {
    $errorMsgs = $validator->getErrors();
}

You can also get an error message of a single attribute:

$errorMsgForUserAttr = $validator->getError('username');

As each attribute can have a few error messages, getError() will give you the last message of the corresponding attribute error stack (array).

List of built-in validators:

  • boolean
  • email
  • file
  • required
  • string
  • url
  • phone
  • in
  • number
  • compare
  • unique

For detailed parameter description of each validator, see class methods in valify/validators.

Testing

In order to properly run unit tests, you need to specify path to the composer autoloader file. Then you just issue the phpunit command in terminal under valify (component root) directory.

Examples

Check index.php in examples directory to view framework in action.

All bug and issue reports are welcome as well as improvement proposals. Enjoy.

xphoenyx/valify 适用场景与选型建议

xphoenyx/valify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 57 次下载、GitHub Stars 达 23, 最近一次更新时间为 2015 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「validator」 「validation」 「validate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 xphoenyx/valify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 xphoenyx/valify 我们能提供哪些服务?
定制开发 / 二次开发

基于 xphoenyx/valify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 23
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-14