cyve/json-schema-form-bundle

从 JSON schema 创建 Symfony 表单

安装次数: 1,245

依赖项: 0

建议者: 0

安全: 0

星标: 7

关注者: 4

分支: 7

开放问题: 3

类型:symfony-bundle

1.0.5 2024-05-17 10:30 UTC

This package is auto-updated.

Last update: 2024-09-17 11:23:05 UTC


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 规范