kingSquare / json-schema-form
此包已 弃用 且不再维护。未建议替代包。
此包最新版本(0.6)的许可证信息不可用。
一个基于json-schema生成简单表单的无框架PHP实现
0.6
2014-07-10 12:27 UTC
Requires
- justinrainbow/json-schema: 1.3.*
- twig/twig: 1.*
This package is not auto-updated.
Last update: 2024-07-02 02:07:24 UTC
README
根据JSON Schema生成表单
一个基于json-schema生成简单表单的无框架PHP实现。此包与版本4兼容,并且可以与
- 客户端验证(例如 http://jqueryvalidation.org/)
- 服务器端验证(例如 https://packagist.org.cn/packages/justinrainbow/json-schema)
安装
库
$ wget https://getcomposer.org.cn/composer.phar
$ php composer.phar install --save kingsquare/json-schema-forms
用法
<?php // Get the schema and data as objects $retriever = new JsonSchema\Uri\UriRetriever; $schema = $retriever->retrieve('file://' . realpath('schema.json')); // Generate $formGenerator = new JsonSchemaForm\Generator($schema); echo $formGenerator->render(); ?>
查看 examples
文件夹以获取更多选项。
需要额外的样式和JavaScript才能正确展示和验证。
处理表单数据
要验证表单中的任何数据是否符合模式,表单数据应该转换为正确的数据类型。表单数据的验证可能如下所示
<?php $validator = new JsonSchema\Validator(); $dataParser = new JsonSchemaForm\DataParser(); //cast any posted form-data (strings) to the proper data-types $data = $dataParser->parse($_POST['root'], $schema); $validator->check($data, $this->schema); $message = "The supplied JSON validates against the schema.\n"; if (!$validator->isValid()) { $message = "JSON does not validate. Violations:\n"; foreach ($validator->getErrors() as $error) { $message .= print_r($error, true); } } echo $message;
运行测试
$ phpunit