sad_spirit / quickform2-json-renderer
A renderer for pear/HTML_QuickForm2 serializing the form so that it can be rebuilt on client side
Package info
github.com/sad-spirit/quickform2-json-renderer
pkg:composer/sad_spirit/quickform2-json-renderer
Requires
- php: ^7.4 | ^8
- pear/html_quickform2: ^2.3.2
Requires (Dev)
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-29 18:18:13 UTC
README
This is a renderer implementation for HTML_QuickForm2 package
that converts an instance of HTML_QuickForm2 to JSON. The resultant JSON representation can be used to rebuild
the form on client side allowing seamless integration with JavaScript frameworks.
Two main differences between this renderer and built-in HTML_QuickForm2_Renderer_Array:
- The latter returns generated HTML for form elements and the former does not;
- This renderer does not generate JavaScript code to insert into page, it provides parts that can be used to perform client-side validation within framework.
Usage
use sad_spirit\html_quickform2\json_renderer\JsonRenderer; use Symfony\Component\HttpFoundation\JsonResponse; use HTML_QuickForm2_Renderer as Renderer; Renderer::register('json', JsonRenderer::class); $form = new \HTML_QuickForm2('form-id'); // // ... building the form ... // if ($form->validate()) { // // ... processing the form ... // return new JsonResponse(['success' => true]); } /** @var JsonRenderer $renderer */ $renderer = $form->render(Renderer::factory('json')); return new JsonResponse($renderer->toArray());
JSON Structure
Form JSON
The array returned by the above $renderer->toArray() has the following structure:
{
// Attributes of the <form> tag
"attributes": {
"attribute name": "attribute value"
},
// Recursive array of form elements
"elements": [],
// Display values for elements
"values": {
"element name": "element value"
},
// Server-side validation errors
"errors": {
"element id": "validation error"
},
// Array of hidden form elements if 'group_hiddens' option is true
"hidden": [],
// Whether form has required fields
"hasRequired": true/false,
// Array of client-side validation rules (or rather parts used to build these)
"rules": []
}
Common Element JSON
Hidden Elements
JSON for <input type="hidden" /> and similar elements using renderHidden() method only
contains two fields:
{
// Element type, usually the string returned by its getType() method
"type": "element type",
// Element attributes
"attributes": {
"attribute name": "attribute value"
}
}
Visible Elements
If the element uses renderElement(), its JSON serialization will contain the following fields
in addition to the above "type" and "attributes":
{
// Whether element is frozen
"frozen": true/false,
// Whether element is required
"required": true/false,
// Label, if one was added
"label": "element label (usually a string)",
// If a style was added by setStyleForId(), it will be passed here
"style": mixed
}
Containers
JSON for containers has an "elements" field in addition to fields for visible elements
{
// Contained elements
"elements": []
}
Additional Element Fields
Elements Having getContent() method
If the element has getContent() method, its return value will be added as "content" field:
{
"content": "element content (usually a string)"
}
Select
<option> and <optgroup> attributes are serialized to the "options" field
{
"options": [
// Represents <option> tag
{
"text": contents of <option> tag,
"attributes": {
// attributes of <option> tag
}
},
// Represents <optgroup> tag
{
// nested <option> tags
"options": [],
"attributes": {
// attributes of <optgroup> tag
}
}
]
}
Static
Return value of getTagName() will be added as "tagName" field
{
"tagName": "string or null"
}
Groups
In addition to common container fields JSON for groups may contain a "separator" field
if getSeparator() returns a non-null value
{
"separator": "string or array"
}
Repeat Elements
JSON serialization for Repeat elements contain the following in addition to common container fields
{
// Element returned by getPrototype() method
// The name is not "prototype" as the latter has special meaning in Javascript
"repeatable": {},
// As returned by getIndexes() method, array of strings
"indexes": [],
// Rules template, as given to constructor of qf.elements.Repeat JS class
"rules": "rules template",
// Validation triggers, as given to constructor of qf.elements.Repeat JS class
"triggers": []
}
Limitations
The renderer does not support
- Elements that contain JS setup code added via
HTML_QuickForm2_JavascriptBuilder::addElementJavascript()other thanHTML_QuickForm2_Container_Repeat; HTML_QuickForm2_Element_Scriptelements used to insert inline JS into form.
These should be reimplemented client-side using the framework of choice.