cyve/json-schema-form-bundle

Create Symfony forms from JSON schema

Installs: 1 245

Dependents: 0

Suggesters: 0

Security: 0

Stars: 7

Watchers: 4

Forks: 7

Open Issues: 3

Type:symfony-bundle

1.0.4 2020-01-16 08:38 UTC

This package is auto-updated.

Last update: 2024-04-13 17:35:53 UTC


README

Creates a Symfony form from a JSON schema.

Installation:

With Composer:

composer require cyve/json-schema-form-bundle

Usage

use Cyve\JsonSchemaFormBundle\Form\Type\SchemaType;
use Cyve\JsonSchemaFormBundle\Validator\Constraint\Schema;

$json = <<<JSON
{
  "$schema": "http://json-schema.org/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)]]);

The form option data_schema MUST be an object representing a JSON schema.

Documentation

Form generation

JSON schema property Symfony FormType Form options
type: "*" and enum: [*] ChoiceType choices is set with the content of enum
type: "array" CollectionType allow_add, allow_delete and delete_empty are set to true. entry_type and entry_options are resolved from the items sub-schema
type: "object" SchemaType data_schema is set with the object sub-schema
type: "integer" IntegerType
type: "number" NumberType
type: "boolean" CheckboxType
type: "string" and format: "date-time" DateTimeType input_format is set to "c" (ISO 8601)
type: "string" and format: "date" DateType input_format is set to "Y-m-d"
type: "string" and format: "time" TimeType input_format is set to "H:i:s"
type: "string" and format: "email" EmailType
type: "string" and format: "uri" UrlType
type: "null" null

The form option label is set with JSON property title if defined
The form option help is set with JSON property description if defined
The form option empty_data is set with JSON property default if defined

Validation

To validate the form subject against the JSON schema, add the form option 'constraints' => [new Cyve\JsonSchemaFormBundle\Validator\Constraint\Schema($schema)] to the root form. The validator uses propertyPath to display the violation messages on the proper fields.
The JSON schema validation is made using justinrainbow/json-schema.
See the JSON schema specification