boomerang/validatr

简单、多语言、独立的PHP验证类 - Validatr。

v3.0.0-alpha 2016-04-06 13:28 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:47:46 UTC


README

#Validatr

Build Status

Validatr是一个简单的多语言PHP验证库,用于检查用户数据。

  • 简单的规则和错误消息表单
  • 灵活且可扩展的库,支持您的回调规则
  • 享受乐趣!

Validatr是一个独立的PHP类,可以根据您的需求进行扩展。

目录

入门

  1. 需要PHP >= 5.4
  2. 按照您方便的方式安装Validatr
  3. 使用Validatr规则检查您的数据
  4. 享受乐趣!

安装

  1. 通过composer
{
    "require": {
        "boomerang/validatr": "~3.0"
    }
}

要下载库,请运行以下命令

$ php composer.phar require boomerang/validatr

$ composer require boomerang/validatr
  1. Git克隆
git clone git@github.com:booomerang/Validatr.git
  1. 下载

下载.zip文件

解压并将其目录复制到您的PHP项目目录中。

如何使用

简单表单

<form action="post.php" method="POST">

    <input type="text" name="name" id="name" placeholder="Name" />
    <input type="password" name="password" id="password" placeholder="Name" />
    <input type="email" name="email" id="email" placeholder="Name" />

    <label for="checkbox">Checkbox</label>
    <input type="hidden" name="checkbox" value="0" />
    <input type="checkbox" name="checkbox" id="checkbox" value="1" /><br/>

    <input type="submit" />

</form>

使用Validatr类的表单处理器

// Set rules for some fields in form
$rules = [
    'name' => 'required|min:3|max:15|equal:Awesome:[s]',
    'password' => 'required|min:3|custom_equal:OK', // Custom rule, defined below
    'email' => 'required|email',
    'checkbox' => 'bool'
];

// Your error messages on your language
$messages = array (
    'name' => array (
        'required' => 'Поле обязательно для заполнения',
        'min' => 'Minimum required 3 characters',
        'max' => 'Maximal zulässige fünfzehn Zeichen',
        'equal' => 'Значение должно равняться - Awesome'
    ),
    'password' => array (
        'required' => 'Поле обязательно для заполнения',
        'min' => 'Minimalement acceptables 3 caractères',
        'custom_equal' => 'Кастомное сообщение для правила custom_equal'
    ),
    'checkbox' => array (
        'bool' => 'Вы не выставили галочку!'
    )
);

$validator = new \Validatr\Validator();

// Adding custom rule
$validator->addRule('custom_equal', function($dataValue, $ruleValue){
    return ($dataValue == $ruleValue) ? true : false;
});

$result = $validator->validate($_POST, $rules, $messages);

echo "<pre>";
print_r($result);
echo "</pre>";

设置消息

//待办

返回值

方法validate()返回两种类型之一

一个数组,包含表单字段名称作为键,包含验证规则和错误消息的嵌套关联数组作为值。(见下例)。

如果验证成功,则返回TRUE的布尔值。

返回值示例

如果所有字段都是空的,则结果可能是

Array
(
    [name] => Array
        (
            [required] => Поле обязательно для заполнения
            [min] => Minimum required 3 characters
            [equal] => Значение должно равняться - Boss
        )

    [password] => Array
        (
            [required] => Поле обязательно для заполнения
            [min] => Minimalement acceptables 3 caractères
        )

    [email] => Array
        (
            [required] => This field is required
            [email] => Invalid email address
        )

    [checkbox] => Array
        (
            [bool] => Вы не выставили галочку!
        )

)

如果所有字段都成功验证,则

1

可用规则

- required  // Checks if field's value is not empty
- min       // Checks if the number of characters of field's value not less than rule value (UTF-8)
- max       // Checks if the number of characters of field's value not greater than rule value (UTF-8)
- email     // Checks if field's value is a valid email adress
- numeric   // Checks if field contains only numeric value
- bool      // Checks if field's value is boolean
- alpha     // Checks if field's value contains only alphabetic characters (UTF-8)
- alnum     // Checks if field's value contains only alphabetic and numeric characters (UTF-8)
- alnumWith // Checks if field's value contains only alphabetic and numeric characters (UTF-8) and some else custom characters
- in        // Checks if value is included in the given list of values.
- notIn     // Checks if value is not included in the given list of values.
- equal     // checks if value is equal to rule value (Strict or not)
- notEqual  // checks if value is bot equal to rule value (Strict or not)

创建您自己的验证规则

要创建您自己的验证规则,请使用addRule方法。

// Lets create our own 'equal' rule.

$data = array (
    'name' => 'ok'
);

$rules = array(
    'name' => 'custom_equal:OK:strict:upper'
);

$messages = array(
    'name' => array(
        'custom_equal' => 'The value must be equal "ok"'
    )
);

// The callback function may receives three arguments:
// 1st arg - field value - 'ok' (From $data)
// 2nd arg - rule value - 'OK' (From $rules 2nd param)
// 3rd arg - additional params - array (From $rules starting 3nd and more params)

// $params in this example is an array - array('strict', 'upper'); But not used
// It should return a boolean value indicating whether the value is valid.
$validator->addRule('custom_equal', function($dataValue, $ruleValue, $params){
    return ($dataValue == $ruleValue) ? true : false;
});

$result = $validator->validate($data, $rules, $messages);

贡献

  1. 分叉它
  2. 创建您的功能分支(git checkout -b my-new-feature)
  3. 进行更改
  4. 为您的新功能编写单元测试(phpunit)
  5. 提交您的更改(git commit -am '添加一些功能')
  6. 将更改推送到分支(git push origin my-new-feature)
  7. 创建新的拉取请求

版本控制

Validatr根据语义版本控制指南进行维护。有时我们可能会出错,但我们将尽可能地遵守这些规则。

发布将使用以下格式进行编号

<major>.<minor>.<patch>

并且按照以下指南构建

  • 破坏向后兼容性会增加主要版本,同时重置次要版本和补丁
  • 没有破坏向后兼容性的新功能会增加次要版本,同时重置补丁
  • 错误修复和其它更改只会增加补丁

有关SemVer的更多信息,请访问http://semver.org/

大胆地从Bootstrap的README.md复制过来 =)

作者

Alex D.

灵感来源于

版权和许可

版权所有2016 Alex D,许可协议MIT