InputfieldJson
JSON viewer and editor
Supports five display/edit modes. The value is always stored and returned as a JSON string, but may be set as a PHP array or object (auto-encoded on assignment).
$f = $modules->get('InputfieldJson');
$f->attr('name', 'my_json');
$f->label = 'My JSON';
$f->mode = InputfieldJson::modeTree; // editable tree (default)
$f->val('{"key":"value"}');
$form->add($f); See Inputfield for the shared Inputfield API.
Expand all Collapse all API reference
The value is always a JSON string. Setting it as an array or object auto-encodes it. An invalid JSON string is silently rejected and the previous value is kept.
// Set as a string
$f->val('{"a":1,"b":2}');
// Set as an array (auto-encoded)
$f->val(['a' => 1, 'b' => 2]);
// Get always returns a string
$json = $f->val(); // '{"a":1,"b":2}'When a form is submitted, the incoming value is validated with json_decode(). If
parsing fails, an error is added and the previous value is restored.
| Constant | Value | Description |
|---|---|---|
Inputfield | 'tree' | Editable tree with add/remove/move (default) |
Inputfield | 'form' | Editable form view of the tree |
Inputfield | 'text' | Raw text editor |
Inputfield | 'code' | Raw text editor with syntax highlighting |
Inputfield | 'view' | Read-only tree (also used by renderValue()) |
$f->mode = InputfieldJson::modeCode; // raw editor with syntax help
$f->mode = InputfieldJson::modeView; // read-only tree | Property | Type | Default | Description |
|---|---|---|---|
mode | string | 'tree' | Editor mode — one of the mode constants above |
useMainMenuBar | bool | false | Show the jsoneditor main menu bar |
useNavigationBar | bool | false | Show the jsoneditor navigation bar |
useSearch | bool | false | Enable search (requires useMainMenuBar; avoid in PW admin) |
renderValue() always uses modeView (read-only tree) regardless of the configured
mode, making it safe to use in contexts where editing is not intended.
echo $f->renderValue();- The editor validates that submitted input is valid JSON, but does not sanitize the content. Treat values from editable instances as untrusted input.
useSearchrequiresuseMainMenuBarto be enabled and does not render well in the ProcessWire admin due to z-index conflicts.- Source file:
wire/modules/Inputfield/InputfieldJson/Inputfield Json.module.php
API reference: methods, properties, hooks
$json = json_encode([
'array' => [1, 2, 3],
'boolean' => true,
'null' => null,
'number' => 123,
'object' => ['a' => 'b', 'c' => 'd'],
'string' => 'Hello World'
]);
// Example of viewable JSON
$f = $modules->get('InputfieldJson');
$f->attr('name', 'view_json');
$f->label = 'View JSON';
$f->mode = 'view'; // options: tree, form, text, code, view
$f->val($json);
$form->add($f);
// Example of editable JSON
$f = $modules->get('InputfieldJson');
$f->attr('name', 'edit_json');
$f->label = 'Edit JSON';
$f->mode = 'tree'; // options: tree, form, text, code, view
$f->val($json);
$form->add($f); Warning: editable JSON can contain anything. This Inputfield only validates that it is valid JSON, not that it is safe JSON for your intended purpose.
Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Inputfield class also inherits all the methods and properties of: Inputfield, WireData and Wire.
Common
| Name | Return | Summary | |
|---|---|---|---|
Inputfield Inputfield Inputfield | InputfieldWrapper | Configuration | |
Inputfield Inputfield Inputfield | None | ||
Inputfield Inputfield Inputfield | string | Is given JSON bad? | |
Inputfield Inputfield Inputfield | self Inputfield | Process input | |
Inputfield Inputfield Inputfield | string | Render Inputfield | |
Inputfield Inputfield Inputfield | string | Render just the value (not input) in text/markup for presentation purposes | |
Inputfield Inputfield Inputfield | self | Set attribute |
Properties
| Name | Return | Summary | |
|---|---|---|---|
| Inputfield | string | One of: 'tree', 'form', 'text', 'code', 'view'. See mode* constants. DEFAULT: tree | |
| Inputfield | bool int | Show a main menu bar? DEFAULT: false | |
| Inputfield | bool int | Show a navigation bar? DEFAULT: false | |
| Inputfield | bool int | Enable search function? Requires $useMainMenuBar, doesn't work well in PW admin. DEFAULT: false | |
| Inputfield | string array | JSON value always returned as a string, but may be optionally set as an array. |
Additional methods and properties
In addition to the methods and properties above, Inputfield
API reference based on ProcessWire core version 3.0.269