Download the PHP package myparcelcom/json-schema-form-builder without Composer
On this page you can find all versions of the php package myparcelcom/json-schema-form-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download myparcelcom/json-schema-form-builder
More information about myparcelcom/json-schema-form-builder
Files in myparcelcom/json-schema-form-builder
Package json-schema-form-builder
Short Description A library that provides an object-oriented API for constructing forms, which can then be converted into a JSON Schema.
License Apache-2.0
Informations about the package json-schema-form-builder
Json Schema Form Builder
This library provides an object-oriented API for constructing forms, which can be converted into a JSON Schema.
This package includes the following functionalities:
- Constructing forms and rendering them as
JSON Schema. - Creating simple fields such as
text,numberandcheckbox. - Creating choice fields such as
select,radioandmulti-select. - Grouping form fields (
groupelement). - Adding translations to form elements using the
LabelTranslationCollectionclass. This includes:- Field label translations (for all fields and groups).
- Option labels (for fields with a finite set of options such as
SelectandRadio).
- Defining values for form fields.
PHP 8
The minimum PHP version is 8.4. To update dependencies on a system without PHP 8 use:
Run tests
You can run the test through docker:
Usage
The following section provides examples of how to use the library to create forms and convert them into JSON Schema.
Meta Fields
While JSON Schema is effective for laying out form structures,
it has limitations in the information it allows to specify. To address this,
properties that fall outside the scope of the JSON Schema specification will
be stored as meta-fields.
Setting these fields is handled using the Meta.php which currently supports the following:
help: A string that describes the purpose of the field.label_translations: Translations for the label of the field. (the non-translated label is stored as thedescriptionproperty)field_type: Used to differentiate between fields that have the same JSON Schema format. Only used for choice fields at the momentenum_labels: Used to define labels for field options. (The keys are stored in theenumproperty) Only for choice fieldsenum_label_translations: Used to define translations for labels of field options. Only for choice fields
Example: Constructing a Form and Converting it to a JSON Schema
The resulting JSON Schema will look as follows:
Example: Setting values
In some cases, you may require setting a value on a form element during its construction. This includes:
- Populating the field with values previously set by the user
- Default values
To support this, form elements allow setting a value property. These values can be obtained in a form by calling the
getValues() method on the Form.php class. This method returns an associative array with the field names as keys and their respective values.
This means that when serialized to JSON, it will be rendered as a JSON object (see example below).
When serialized to JSON, the resulting array will look as follows:
Example: Grouping elements
The resulting JSON Schema property for the group will look as follows:
NOTE: When defining groups, required properties that are its direct children will be added to its required property rather than the required property of the parent form.
Values in Groups
If children of a group have a value set, their values will be rendered as a nested structure. For example:
When serialized, getValues() will result in the following JSON object:
Example: Adding translations to a form element's label
The resulting JSON Schema property will look as follows:
Example: Adding translations to a Form element's labels and option labels
The resulting JSON Schema property will look as follows:
Example: Multi-Select
The resulting JSON Schema property will be of type array and look as follows:
All versions of json-schema-form-builder with dependencies
laravel/framework Version >=12.0
ramsey/uuid Version ^4.7
opis/json-schema Version ^2.6