cyve / json-schema-form-bundle
Create Symfony forms from JSON schema
Installs: 1 246
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 4
Forks: 8
Open Issues: 3
Type:symfony-bundle
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
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
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