jimlei/formhandler
Composer 安装命令:
composer require jimlei/formhandler
包简介
...
README 文档
README
Include with composer
composer require jimlei/formhandler:dev-master
Create the object/entity that will be modified by the form (request)
<?php // src/Entity/Article.php namespace Acme\Entity; class Article { private $id; private $title; public function getId() { return $this->id; } public function setId($id) { $this->id = $id; return $this; } public function getTitle() { return $this->title; } public function setTitle($title) { $this->title = $title; return $this; } }
Create the form that will map/validate the data in the request
<?php // src/Form/ArticleForm.php namespace Acme\Form; use Acme\Entity\Article; use Jimlei\FormHandler\Form; class ArticleForm extends Form { public function __construct(Article $article) { $fields = array( 'title' => array( 'type' => 'string', 'maxLength' => '60', 'required' => true ) ); parent::__construct($article, $fields); } }
It's depending on the Request->getData method so you should implement the RequestInterface in your handling of request data (method, data, query, etc).
<?php // src/Net/Request.php namespace Acme\Net; use Jimlei\FormHandler\RequestInterface; /** * Maps a request to usable data. */ class Request implements RequestInterface { private $data; public function __construct() { $this->data = json_decode(file_get_contents('php://input')) ?: array(); } public function getData() { return $this->data; } }
Bring it together
<?php // index.php use Acme\Entity\Article; use Acme\Form\ArticleForm; use Acme\Net\Request; require 'vendor/autoload.php'; $request = new Request(); $article = new Article(); $form = new ArticleForm($article); $form->handleRequest($request); if ($form->isValid()) { // save article... } // do something with the errors foreach ($form->getErrors() as $error) { // log, add to flash message, display otherwise, etc. }
You can do curl requests to test it out. Play around with passing parameters, change the form field types/require/length, etc and see validation and errors change.
curl --data '{"title":"foo"}' localhost
Available types
- bool
datedatetime- float
- int
- ip
- string
time- timestamp
- url
Available validations
- required (bool)
- min (int)
- max (int)
- minLength (int)
- maxLength (int)
Run tests
$ vendor/bin/phpunit
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-04-04