deathnerd / php-wtforms
此包已被废弃且不再维护。未建议替代包。
WTForms Python 库的 PHP 重写版本
0.5.2
2016-08-18 18:21 UTC
Requires
- php: >=5.5
- nesbot/carbon: >=1.18 <2.0.0
Requires (Dev)
- phpunit/phpunit: 4.8.*
This package is not auto-updated.
Last update: 2020-01-24 16:22:45 UTC
README
php-wtforms
WTForms Python 库的 PHP 重写版本。
我对 WTForms 背后的想法或与其原始实现相关的内容不享有任何版权。我只是将其修改并移植到 PHP。
安装
将以下行添加到您的 composer.json
文件的 require
部分
"deathnerd/php-wtforms":"0.5.2"
或者如果您愿意尝试最新版本,可以拉取 master
分支
"deathnerd/php-wtforms":"dev-master"
或者从您喜欢的终端运行以下命令 composer require deathnerd/php-wtforms:0.5.2
用于稳定版本和 composer require deathnerd/php-wtforms:dev-master
用于最新开发版本。
注意:dev-master 版本可能不稳定。
快速入门
要创建一个简单的登录表单,就像这样:###LogInForm.php
<?php namespace MyNamespace\Forms; use WTForms\Form; use WTForms\Fields\Simple\PasswordField; use WTForms\Fields\Simple\SubmitField; use WTForms\Fields\Core\StringField; use WTForms\Validators\InputRequired; use WTForms\Validators\Length; use WTForms\Validators\Regexp; use MyNamespace\Validators\NotContains; class LogInForm extends Form { public function __construct(array $options = []) { parent::__construct($options); $this->username = new StringField(["validators"=>[ new InputRequired("You must provide a username"), new Length("Usernames must be between %(min)d and %(max)d characters long", ["min"=>3, "max"=>10]), new NotContains("Usernames may not contain the following characters: ;-/@", ["invalid_members"=>[";","-","/","@"]]) ]]); $this->password = new PasswordField(["validators"=>[ new InputRequired("Can't log in without a password!"), new Length("Passwords must be at least %(min)d characters in length", ["min"=>5]) ]]); $this->submit = new SubmitField(["label"=>"Submit"]); } }
###NotContains.php
<?php namespace MyNamespace\Validators; use WTForms\Validators\Validator; use WTForms\Form; use WTForms\Fields\Core\Field; use WTForms\Exceptions\ValidationError; class NotContains extends Validator { /** * @var string|array */ public $invalid_members; /** * @param string $message Error message to raise in case of a validation error * @param array $options */ public function __construct($message = "", array $options = ['invalid_members' => []]) { assert(!empty($options['invalid_members']), "Doesn't make sense to not have any invalid members"); $this->invalid_members = $options['invalid_members']; $this->message = $message; } /** * @param Form $form * @param Field $field * @param string $message * * @return mixed True if the field passed validation, a Validation Error if otherwise * @throws ValidationError */ public function __invoke(Form $form, Field $field, $message = "") { if (strlen($field->data) != strlen(str_replace($this->invalid_members, "", $field->data))) { if ($message == "") { if ($this->message == "") { $message = "Invalid Input."; } else { $message = $this->message; } } throw new ValidationError($message); } return true; } }
###login.php
<?php require_once 'vendor/autoload.php'; use MyNamespace\Forms\LogInForm; $form = LogInForm::create(["formdata" => $_POST]); if ($_POST) { if ($form->validate()) { // do stuff to log in and authenticate the user } } ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>LogIn Form</title> </head> <body> <?php if ($form->errors) { ?> <ul class="errors"> <?php foreach ($form->errors as $field_name => $errors) { foreach ($errors as $field_error) { ?> <li><?= $field_name ?> - <?= $field_error ?></li> <?php } } ?> </ul> <?php } ?> <form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST"> <?= $form->username->label ?> <?= $form->username ?> <?= $form->password->label ?> <?= $form->password ?> <?= $form->submit ?> </form> </body> </html>
就这样。更深入的实际示例和文档将在未来提供。目前,请查看 tests
目录以获取如何实现各种功能的灵感。