wudege/tforms

TForms 是一个适用于 PHP API 项目的灵活表单验证库。

v0.0.2 2017-02-20 04:00 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:45:56 UTC


README

TForms 是一个适用于 PHP API 项目的灵活表单验证库。

Software License Latest Stable Version Total Downloads Twitter URL

安装

  • 使用 composer 命令或 composer.json 文件。这是推荐的方式。SDK 在这里 wudege/tforms
$ composer require wudege/tforms

用法

require __DIR__ . '/../vendor/autoload.php';

class UserEditForm extends \TForms\Form
{
    const GENDER_MALE   = 1;
    const GENDER_FEMALE = 0;

    public $username;
    public $email;
    public $age;
    public $gender;
    public $blog;

    /**
     *
     * @author wudege <hi@wudege.me>
     * @return array
     */
    public function attributeNames()
    {
        return [
            'username',
            'email',
            'age',
            'gender',
            'blog',
        ];
    }

    public function attributeLabels()
    {
        return [
            'username',
            'email',
            'age',
            'gender',
            'blog',
        ];
    }

    public function rules()
    {
        return [
            ['username, email, age, gender, blog', 'required'],
            ['username', 'length', 'min' => 4, 'max' => 32],
            ['email', 'email'],
            ['age', 'numerical', 'min' => 18, 'max' => 28, 'integerOnly' => true],
            ['gender', 'in', 'range' => [self::GENDER_MALE, self::GENDER_FEMALE]],
            ['blog', 'url'],
        ];
    }
}

$form             = new UserEditForm();
$form->attributes = [
    'username' => 'wudege',
    'email'    => 'hi@wudege.me',
    'age'      => 20,
    'gender'   => 1,
    'blog'     => 'https://wudege.me',
];

try {
    $form->validate();
} catch (\TForms\Exception\ValidationException $e) {
    die($e->getMessage());
}
die('done');

测试

$ ./vendor/bin/phpunit tests/TForms/Tests 

许可证

MIT 许可证 (MIT)。 许可证文件