Download the PHP package w3r-one/json-schema-bundle without Composer
On this page you can find all versions of the php package w3r-one/json-schema-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download w3r-one/json-schema-bundle
More information about w3r-one/json-schema-bundle
Files in w3r-one/json-schema-bundle
Package json-schema-bundle
Short Description serialize Symfony Forms into JSON schema
License MIT
Informations about the package json-schema-bundle
JsonSchemaBundle
A bundle to serialize a Symfony Form into a JSON Schema (RFC 2020-12).
Installation
$ composer require w3r-one/json-schema-bundle
If you're not using Symfony Flex, you've to register the bundle manually:
Usage
View the generated JSON Schema
Purpose
The goal behind this bundle is based on the fact that it is complicated for a modern front-end application to maintain a form component that is not mapped directly on a Symfony FormType.
Most of the time, the front-end component is defining form's props in a static way and if the back-end wants to update the form, we need to work twice, it's error prone and it's not extensible at all.
The main idea is to give the lead to the back-end, provide a JSON schema dynamically that will detail the full component and its related documentation ; the front-end "just" have to display and handle the form on his side.
If the Form is changing or even if it's dynamic based on some roles / scopes / etc., the front-end developer have nothing to change.
It's also allow working with forms directly in Twig and in the same time in a Javascript context.
The business rules are not duplicated and are only handled by the back-end.
This bundle doesn't provide any Front-End component, feel free to choose the stack that feet your needs to build your own Javascript Form.
Logic
- Back-End
- Create your FormType as usual (it can include dynamic fiels, ACL, business rules, FormEvents, etc.)
- Extend it if needed (through
w3r_one_json_schemaor your own Transformers) - Optional: sending it to the view and display/test it directly in Twig
- Serialize the Form into a JSON Schema and send it to the view
- Optional: serialize the initial data as well to hydrate your form data
- Front-End
- Create the main form (
options.form.method+options.form.action) - Iterate recursively on all sub properties to create the complete form.
- Map each child with the correct JS component thanks to
options.widget(+options.layoutif needed) - Optional: hydrate each field value with initial data
- Handle submit on XHR (custom HTTP header
X-Requested-With: XMLHttpRequestthanks tooptions.form.async) or normally - Display errors if any - else a flash message / redirect the user
- Create the main form (
Concrete exemple
This example allow to handle a form directly in Twig without XHR AND with async Javascript, feel free to drop completely the twig/not async part.
$this->em is a simple reference to the EntityManagerInterface.
$this->apiSerializer is a simple service based on the Symfony Serializer.
View the service
Architecture
The resolver will traverse the form, guess the right Transformer for each property and apply recursive transformations based on the following schema.
Legend
- Transformers
- Black: Base transformer
- Yellow: Json Schema native transformers
- Orange: Intermediate transformers
- White: Symfony Form Type' transformers
- Relations:
- Red: direct inheritance
- Blue: indirect inheritance depending on a specific FormType's option, e.g.
widgetfor the Date typesmultiplefor the Choice typeinputforNumberTypefractionalforPercentType
You can add your own transformers, or override/extend the transformers of your choice by yourself, see the dedicated section of this readme.
If needed, the form extension allow you to add custom props in w3r_one_json_schema to pass to your json specs.
Translator
This bundle relies on Symfony TranslatorInterface to translate:
label(withlabel_translation_parameters)help(withhelp_translation_parameters)- named enums (from
ChoiceType) attr.title(withattr_translation_parameters)attr.placeholder(withattr_translation_parameters)- error messages
The translation domain is dynamically retrieved from translation_domain option:
- disabled if
false === translation_domain - property scope if
null !== translation_domain - parent scope if any
translation_domainis found recursively
CSRF
If you've installed symfony/security-csrf and enabled crsf_protection on you FormType, the bundle will automatically add the correct csrf property (_token by default) with the default generated value (thanks to the TokenGeneratorInterface) in a hidden widget.
Built-in FormType
All Symfony FormTypes as 6.2 version are supported.
View the complete list
* [TextType](https://symfony.com/doc/current/reference/forms/types/text.html) * [TextareaType](https://symfony.com/doc/current/reference/forms/types/textarea.html) * [EmailType](https://symfony.com/doc/current/reference/forms/types/eamil.html) * [PasswordType](https://symfony.com/doc/current/reference/forms/types/password.html) * [SearchType](https://symfony.com/doc/current/reference/forms/types/search.html) * [UrlType](https://symfony.com/doc/current/reference/forms/types/url.html) * [TelType](https://symfony.com/doc/current/reference/forms/types/tel.html) * [ColorType](https://symfony.com/doc/current/reference/forms/types/color.html) * [FileType](https://symfony.com/doc/current/reference/forms/types/file.html) * [RadioType](https://symfony.com/doc/current/reference/forms/types/radio.html) * [UuidType](https://symfony.com/doc/current/reference/forms/types/uuid.html) * [UlidType](https://symfony.com/doc/current/reference/forms/types/ulid.html) * [HiddenType](https://symfony.com/doc/current/reference/forms/types/hidden.html) * [IntegerType](https://symfony.com/doc/current/reference/forms/types/integer.html) * [MoneyType](https://symfony.com/doc/current/reference/forms/types/money.html) * [NumberType](https://symfony.com/doc/current/reference/forms/types/number.html) * [PercentType](https://symfony.com/doc/current/reference/forms/types/percent.html) * [RangeType](https://symfony.com/doc/current/reference/forms/types/range.html) * [ChoiceType](https://symfony.com/doc/current/reference/forms/types/choice.html) * [EnumType](https://symfony.com/doc/current/reference/forms/types/enum.html) * [EntityType](https://symfony.com/doc/current/reference/forms/types/entity.html) * [CountryType](https://symfony.com/doc/current/reference/forms/types/country.html) * [LanguageType](https://symfony.com/doc/current/reference/forms/types/language.html) * [LocaleType](https://symfony.com/doc/current/reference/forms/types/locale.html) * [TimezoneType](https://symfony.com/doc/current/reference/forms/types/timezone.html) * [CurrencyType](https://symfony.com/doc/current/reference/forms/types/currency.html) * [DateType](https://symfony.com/doc/current/reference/forms/types/date.html) * [DateTimeType](https://symfony.com/doc/current/reference/forms/types/datetime.html) * [TimeType](https://symfony.com/doc/current/reference/forms/types/time.html) * [WeekType](https://symfony.com/doc/current/reference/forms/types/week.html) * [BirthdayType](https://symfony.com/doc/current/reference/forms/types/birthday.html) * [DateIntervalType](https://symfony.com/doc/current/reference/forms/types/dateinterval.html) * [CollectionType](https://symfony.com/doc/current/reference/forms/types/collection.html) * [CheckboxType](https://symfony.com/doc/current/reference/forms/types/checkbox.html) * [ButtonType](https://symfony.com/doc/current/reference/forms/types/button.html) * [ResetType](https://symfony.com/doc/current/reference/forms/types/reset.html) * [SubmitType](https://symfony.com/doc/current/reference/forms/types/submit.html) * [RepeatedType](https://symfony.com/doc/current/reference/forms/types/repeated.html)Supported JSON Schema specs
- $schema (
https://json-schema.org/draft/2020-12/schema) - $id (
{host}/schemas/{formType}.json) - type (
object|array|string|number|integer|bool) - title (FormType name at parent level,
labelFormType's option at child level or empty string if thelabelis set tofalse) - description (FormType
help_messageFormType's option) - properties (children properties)
- enum (constant values)
- readOnly (if
disabledFormType's option is set totrue) - writeOnly (if
mappedFormType's option is set tofalse) - default (if
dataFormType's option is defined) - uniqueItems (
truefor an array)
Unsupported JSON Schema specs (for now)
- required (need to be guessed from
Doctrinetype + potentialValidatorasserts) - minItems|maxItems (need to be guessed from assert
Count) - exclusiveMinimum|minimum|exclusiveMaximum|maximum (need to be guessed from assert
GreaterThanOrEqual,GreaterThan,LowerThanOrEqual&LowerThan) - minLength|maxLength (need to be guessed from
Doctrinetype + assertLength) - pattern (need to be guessed from assert
Regex) - schema composition
- format (?)
Additional JSON Schema specs
All non standards properties are wrapped into options property.
It includes:
widgetproperty to identify the widget behind the component- it's the FormType name in snake_case, basically
CustomCollectionTypewill give you the widgetcustom_collection - the only exception is related to all date's FormTypes where we suffix the javascript widget by the
widgetFormType's option. It can give you very different components:DateTypedate_choicedate_textdate_single_text
DateTimeTypedate_time_choicedate_time_textdate_time_single_text
TimeTypetime_choicetime_texttime_single_text
WeekTypeweek_choiceweek_textweek_single_text
BirthdayTypebirthday_choicebirthday_textbirthday_single_text
DateIntervalTypedate_interval_choicedate_interval_textdate_interval_integerdate_interval_single_text
- it's the FormType name in snake_case, basically
layoutproperty to apply a specific layout to the component (defaultdefault, can be overridden byw3r_one_json_schema.layout)attrall the HTML attributes defined in the FormType- at parent level, a
formproperty with:options.form.method(string: the method of your form - defaultPOST)options.form.action(string: the action behind your form - default current URI)options.form.async(bool: if you want your form in XMLHttpRequest - default true, can be overridden byw3r_one_json_schema.xmlHttpRequest)
- all others settings related to a specific FormType:
CheckboxTypeoptions.checkbox.value
ChoiceTypeoptions.choice.expandedoptions.choice.multipleoptions.choice.filterableoptions.choice.placeholderoptions.choice.preferredChoicesoptions.choice.enumTitles
CollectionTypeoptions.collection.allowAddoptions.collection.allowDelete
CountryTypeoptions.choice.alpha3
DateType/DateTimeType/TimeType/WeekType/BirthdayType/DateIntervalTypeoptions.date_time.formatoptions.date_time.inputoptions.date_time.inputFormatoptions.date_time.modelTimezoneoptions.date_time.placeholder
IntegerTypeoptions.integer.roundingMode
LanguageTypeoptions.choice.alpha3
MoneyTypeoptions.money.currencyoptions.money.divisoroptions.money.roundingModeoptions.money.scale
NumberTypeoptions.number.roundingModeoptions.number.scale
PasswordTypeoptions.password.alwaysEmpty
PercentTypeoptions.percent.symboloptions.percent.typeoptions.percent.roundingModeoptions.percent.scale
RadioTypeoptions.radio.valueoptions.radio.falseValues
If you want to pass other specific properties to your component, feel free to wrap them into w3r_one_json_schema property.
For example:
Override / Extend
You can totally override or extend any transformer / json specs of this bundle.
Widget / Layout resolving
In your FormTypes, you can override any widget / layout of your choice thanks to the w3r_one_json_schema option.
For example:
You can also override the default layout globally if needed:
FormType Transformers
You can register your own transformers.
Tag them with the name w3r_one_json_schema.transformer and define the form_type you want to transform.
Your transformers are resolved before ours, so if you override an existing transformer, it'll be executed in place of the bundle built-in ones.
Transformers must implement the TransformerInterface.
The proper approach is to extend one of ours abstract or specific transformers, redefine method transform, call the parent function and extending/overwriting the json schema before returning it.
You can also implement directly the interface, but you've to manage everything by yourself in this case.
Example 1
You're using VichUploaderBundle and you want to serialize specific options of this bundle.
Just extend the ObjectTransformer, call the parent function, embed your json props and voila!
Example 2
You want to add a PositionType as an integer.
Here we just extend the correct base IntegerTransformer.
Example 3
You want to override the TextareaType to replace it by a rich-text / wysiwyg editor.
Note that a better approach would have been to use a WysiwygType and to create a specific WysiwygTypeTransformer.
All versions of json-schema-bundle with dependencies
doctrine/inflector Version >=1.4
symfony/config Version >=4.4
symfony/dependency-injection Version >=4.4
symfony/finder Version >=4.4
symfony/form Version >=4.4
symfony/http-foundation Version >=4.4
symfony/http-kernel Version >=4.4
symfony/intl Version >=4.4
symfony/security-csrf Version >=4.4
symfony/translation Version >=4.4