tyurderi/validator

简单的 PHP 表单验证器

1.0.1 2016-05-31 15:00 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:47:33 UTC


README

这是我简单 PHP 验证器类的版本。

安装

composer require tyurderi/validator

用法

$v = new Validator\Validator();

$v->addRule('unique_username', function($fields, $value, $params) {
    // logic goes here
    return true;
});

$v->add('username', 'tyurderi', 'required|min:3|max:30|unique_username', array(
    'required'        => 'The username is required.',
    'min'             => 'The username should be at least 3 characters long.',
    'max'             => 'The username should be at most 30 characters long.',
    'unique_username' => 'The username is already in use.'
));

$v->validate();

if ($v->passes())
{
    echo 'Validation was successfully!';
}
else
{
    echo 'Validation failed.', PHP_EOL;
    foreach ($v->errors() as $message)
    {
        echo $message, PHP_EOL;
    }
}

可用规则

required

值必须不为空。

in:1,2,3

值应该等于1、2或3。

email

值应该是一个有效的电子邮件地址。

notin:1, 2, 3

值不应该等于1、2或3。

min:3

值应该至少有3个字符长。

max:30

值应该最多有30个字符长。

len:30

值应该正好有30个字符长。

min_value:3

值应该等于或大于3。

max_value:30

值应该等于或小于30。

matches:anotherField

值应该等于另一个已注册字段的值。

is:someValue

值应该等于参数1的值。(someValue)

min_words:3

值应该至少包含3个单词。

许可证

MIT