eredi93 / forms
表单生成和验证
0.3.0
2016-06-07 13:56 UTC
Requires
- php: >=5.5
Requires (Dev)
- phpunit/phpunit: ~4.5
- squizlabs/php_codesniffer: 2.1.*
This package is not auto-updated.
Last update: 2024-09-14 18:47:29 UTC
README
易用且高度可定制的PHP包,用于表单生成和数据验证。支持PSR-7 HTTP-Message。
- 生成HTML表单
- 验证数据
- PSR-7 HTTP-Message
注意:此包在Slim3下开发,目前只支持此框架。
安装
使用Composer安装。
{
"require": {
"eredi93/forms": "0.2.*"
}
}
基本用法(Slim 3)
扩展基本表单,在setUp方法中设置表单字段
use Forms\From; use Forms\Validators\Required; class LoginForm extends Form { protected function setUp() { $this->fields = [ (new TagOpen(['class' => "wrapper"])), (new TextInput("identifier")) ->setLabel("User / Email") ->setAutocomplete("off") ->setDecoratorClass("input-field col s12") ->setValidators([ new Required(), ]), (new PasswordInput("password")) ->setLabel("Password") ->setDecoratorClass("input-field col s12") ->setValidators([ new Required(), ]), (new SubmitButton()) ->setDecoratorClass("input-field col s12") ->setClass("btn yellow right") ->setText("Login"), (new TagClose(), ]; } }
现在你已经创建了表单,在GET控制器中实例化它
public function login(Request $request, Response $response, $args) { // Instantiate Form $form = new LoginForm(); /** * You can pass to the form the method action and the CSRF name * by default the form uses * - $action="" * - $method="POST" * - $csrf=['csrf_name', 'csrf_value'] */ // Render Form echo $form->render($request); return $response; }
在POST控制器中调用validate方法以验证表单
public function loginPost(Request $request, Response $response, $args) { // Instantiate Form $form = new LoginForm(); // Check validation if ($form->validate($request)) { /* * Form validation passed log in the user */ } // Render Form with validation echo $form->render($request); return $response; }
贡献
请在GitHub上提交问题,或者如果你想直接贡献,请提交pull request。
运行测试
使用phpunit运行测试。运行./vendor/bin/phpunit以运行测试。