cyve / json-schema-form-bundle
从 JSON schema 创建 Symfony 表单
1.0.5
2024-05-17 10:30 UTC
Requires
- php: ^7.1
- justinrainbow/json-schema: ^5.0
- symfony/form: ^4.0|^5.0
- symfony/options-resolver: ^4.0|^5.0
- symfony/validator: ^4.0|^5.0
Requires (Dev)
- phpunit/phpunit: ^8.5
README
从 JSON schema 创建 Symfony 表单。
安装
使用 Composer
composer require cyve/json-schema-form-bundle
用法
use Cyve\JsonSchemaFormBundle\Form\Type\SchemaType; use Cyve\JsonSchemaFormBundle\Validator\Constraint\Schema; $json = <<<JSON { "$schema": "https://json-schema.fullstack.org.cn/draft-07/schema#", "$id": "http://example.com/product.schema.json", "title": "Product", "type": "object", "properties": { "id": { "type": "integer" }, "name": { "type": "string" } }, "required": ["id", "name"] } JSON; $schema = json_decode($json); $subject = new \StdClass(); $form = $container->get('form.factory')->create(SchemaType::class, $subject, ['data_schema' => $schema, 'constraints' => [new Schema($schema)]]);
表单选项 data_schema
必须是一个表示 JSON schema 的 object
。
文档
表单生成
如果定义了,表单选项 label
将使用 JSON 属性 title
设置。
如果定义了,表单选项 help
将使用 JSON 属性 description
设置。
如果定义了,表单选项 empty_data
将使用 JSON 属性 default
设置。
验证
要验证表单主题与 JSON schema 的匹配,请在根表单中添加表单选项 'constraints' => [new Cyve\JsonSchemaFormBundle\Validator\Constraint\Schema($schema)]
。验证器使用 propertyPath
在适当的字段上显示违规信息。
JSON schema 验证使用 justinrainbow/json-schema 进行。
参见 JSON schema 规范