e0ipso / schema-forms
Creates form definitions from JSON Schema property definitions and display configuration.
Installs: 75 943
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 2
Open Issues: 9
Requires
- php: >=7.4
- ext-json: *
- e0ipso/shaper: ^1.2
- justinrainbow/json-schema: ^5.2
- symfony/polyfill-php81: ^1.26
Requires (Dev)
- drupal/coder: ^8.3
- drupal/core-dev: ^9.4
- drupal/core-recommended: ^9.4
- enlightn/security-checker: ^1.7
- ergebnis/composer-normalize: ^2.28
- nikic/php-parser: ^4.10
- php-parallel-lint/php-parallel-lint: ^1.3
- phpro/grumphp-shim: ^1.13
- phpunit/phpunit: ^9.5
- povils/phpmnd: ^2.4
- sebastian/phpcpd: ^6.0
- squizlabs/php_codesniffer: ^3.6
- dev-master
- v2.5.1
- v2.5.0
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.1
- v2.0.0
- v1.0.2
- v1.0.1
- v1.0.0
- 0.1.2
- 0.1.1
- 0.1.0
- dev-dependabot/composer/guzzlehttp/psr7-1.9.1
- dev-dependabot/composer/symfony/http-kernel-4.4.50
- dev-dependabot/composer/twig/twig-2.15.3
- dev-feat/add-delete-button
- dev-fix/move-cleanup-after-build
- dev-docs/update-readme
- dev-fix/correct-type-hint
- dev-feat/type-casting
- dev-feat/default-empty-string
- dev-feat/semantic-release
This package is auto-updated.
Last update: 2024-11-20 00:51:43 UTC
README
Schema forms is a project that aims to generate form structures for the different PHP frameworks based on the data definitions in JSON Schema.
Frameworks supported:
- Drupal.
Usage
Given the following JSON Schema defining the different properties of a user object.
{ "title": "A registration form", "description": "A simple form example.", "type": "object", "required": [ "firstName", "lastName" ], "properties": { "firstName": { "type": "string", "title": "First name", "default": "Chuck" }, "lastName": { "type": "string", "title": "Last name" }, "age": { "type": "integer", "title": "Age", "description": "(earthian year)" }, "bio": { "type": "string", "title": "Bio" }, "password": { "type": "string", "title": "Password", "minLength": 3, "description": "The key to get in." }, "telephone": { "type": "string", "title": "Telephone", "minLength": 10 } }
And the following UI JSON Schema refining the form generation process:
{ "firstName": { "ui:autofocus": true, "ui:emptyValue": "" }, "age": { "ui:widget": "updown", "ui:title": "Age of person" }, "bio": { "ui:widget": "textarea" }, "password": { "ui:widget": "password", "ui:help": "Hint: Make it strong!" }, "date": { "ui:widget": "alt-datetime" }, "telephone": { "ui:options": { "inputType": "tel" } } }
Execute this PHP code:
use SchemaForms\Drupal\FormGeneratorDrupal; $generator = new FormGeneratorDrupal(); $context = new Context(['ui_hints => $ui_schema_data]); $actual_form = $generator->transform($schema_data, $context); // It generates the following Drupal Form API form: [ 'firstName' => [ '#type' => 'textfield', '#title' => 'First name', '#required' => TRUE, ], 'lastName' => [ '#type' => 'textfield', '#title' => 'Last name', '#required' => TRUE, ], 'age' => [ '#type' => 'number', '#title' => 'Age of person', '#description' => '(earthian year)' ], 'bio' => [ '#type' => 'textarea', '#title' => 'Bio', ], 'password' => [ '#type' => 'password', '#title' => 'Password', '#description' => 'Hint: Make it strong!' ], 'telephone' => [ '#type' => 'telephone', '#title' => 'Telephone', ], ];
UI Schema Data
Based on the shape of the data described by the JSON Schema, this library can generate a form. However, there are multiple ways to generate a form for the same shape of data. The UI schema data allows you to control the form elements and inputs that will collect the data in the appropriate way.
Supported UI controls are:
-
$ui_form_data['ui:title']
Controls the label associated to the input element. Defaults to the element's
title
property in the JSON Schema. -
$ui_form_data['ui:help']
Adds a hint to the input element. Defaults to the element's
description
property in the JSON Schema. -
$ui_form_data['ui:placeholder']
Adds a placeholder text to the input.
-
$ui_form_data['ui:widget']
Lets you use al alternative input element. For instance, it lets you use
<select>
instead of<input type="radio">
, or use<input type="hidden">
, among others. -
$ui_form_data['ui:enabled']
If 0 the form element will be rendered as non-interactive.
-
$ui_form_data['ui:visible']
If 0 the form element will not be rendered.
-
$ui_form_data['ui:enum']
Lets you define how the options for selects and radios are populated. By default, the enum information in the schema defines the options. This might not be enough, or even possible.
$ui_form_data['ui:enum']['labels']['mappings']
An object defining the label for each key. Ex:
{"uuid1": "Super duper product"}
.