class JsonForm
Processes forms using configuration stored in a JSON file which specifies database columns, their HTML input fields,
and validation rules.
A generic implementation which should work for most cases is found in ManagedAdminController::_getEditForm
(generates the form) and Controller::saveJsonData (saves the POST submission) Variables
Name | Visibility | Description |
$errors | (static) | |
$id_prefix | (static) | |
$name_format | (static) | |
$next_helptext | (static) | |
$next_label | (static) | |
$next_required | (static) | |
Functions
Name | Visibility | Description |
__callStatic | public (static) | Auto-wrapper around Fb methods |
argReplace | private (static) | Replace magic strings in "args" arrays with various metadata values |
autofillList | public (static) | Returns HTML for an auto-complete list of records |
autofillOptionDefaults | public (static) | Determine the auto-generated default values for an autofill-list |
changeFieldRequired | protected (static) | Modify a JSON form config to change the 'required' status of a particular field |
checkboxList | public (static) | Returns HTML for a list of checkboxes, applying name conversions along the way |
collateData | public (static) | Collates POST data using specified config options |
collateFieldData | protected (static) | Collates a single field's $_POST data for INSERT/UPDATE queries, and performs validation |
convertFieldName | protected (static) | Format a field name as per the specification defined by Form::setFieldNameFormat |
expandItemDefns | public (static) | Expands item definitions for a field pulled from JSON |
fieldAuto | public (static) | Return HTML for a field, with the wrapping HTML detected automatically. |
fieldFieldset | public (static) | Return HTML for a field wrapped in a FIELDSET |
fieldMethodClass | protected (static) | Convert a full method name (e.g. Sprout\Helpers\Fb::text) into a friendly class name |
fieldPlain | public (static) | Return HTML for a 'plain' field, i.e. one which doesn't require a FIELDSET wrapped around it. |
flattenGroups | public (static) | Extract field defns from a list (which may include groups) |
genId | protected (static) | Generate a unique id which should be stable across calls to this URL |
getData | public (static) | Gets the form per-field value for a single field |
getFieldErrors | public (static) | Return the errors for a given field |
html | public (static) | Return HTML which has been wrapped in the form field DIVs |
loadAutofillListData | public (static) | Loads autofill_list data for use on a view |
loadFromSession | public (static) | Load data and errors from the session, with optional record id validation |
loadMultiEditData | public (static) | Loads multiedit data for use on a view |
makeOptional | public (static) | Modify a JSON form config to make a particular field optional |
makeRequired | public (static) | Modify a JSON form config to make a particular field required |
money | public (static) | Returns HTML for a money field, using Fb::money to generate the field itself |
multiradio | public (static) | Returns HTML for a bunch of radiobuttons, using Fb::multiradio to generate the fields |
nextFieldDetails | public (static) | Set the details for the next field which will be outputted. |
number | public (static) | Returns HTML for a number field, using Fb::number to generate the field itself |
out | public (static) | Return content which has been HTML-encoded and wrapped in the form field DIVs |
passString | protected (static) | Returns the first argument |
password | public (static) | Returns HTML for a password field, using Fb::password to generate the field itself |
renderField | public (static) | Renders the input for a field definition pulled from a JSON file |
renderTabItem | public (static) | Render a tab item, which may be a field, heading, html block, etc |
resetField | public (static) | Reset the state machine for field values |
setData | public (static) | Set form per-field values for the fields |
setErrors | public (static) | Set per-field error messages to display |
setFieldIdPrefix | public (static) | Sets the prefix for generated IDs |
setFieldNameFormat | public (static) | Set a format string which will alter the field name prior to being passed to the underlying render method |
setFieldValue | public (static) | Sets the value for a single field |
setParameterForColumns | public (static) | Set a parameter for fields to be a specific value, for one or more columns |
text | public (static) | Returns HTML for a text field, using Fb::text to generate the field itself |
string Form::__callStatic ( string $func , array $args );
Auto-wrapper around Fb methods
Will wrap the Fb method with the same name as the called method, e.g. Form::datepicker wraps Fb::datepicker
Wrapping is done using Form::fieldAuto array JsonForm::argReplace ( array $args , array $metadata );
Replace magic strings in "args" arrays with various metadata values
Replacements:
%% The current record id string Form::autofillList ( string $name , array $attrs , array $options );
Returns HTML for an auto-complete list of records
The form data for this field should be an array of arrays with at least the following keys:
[
'id' => record ID,
'value' => title text visible in the list item,
'orderkey' => ordinal value for record ordering
] array JsonForm::autofillOptionDefaults ( array $auto , string $local_table_name );
Determine the auto-generated default values for an autofill-list
Defaults:
joiner_local_col Singular of local table name + '_id'
joiner_foreign_col Singular of the foreign_table option + '_id'
foreign_label_col 'name'
reorder false void JsonForm::changeFieldRequired ( array &$conf , string $field_name , bool $required );
Modify a JSON form config to change the 'required' status of a particular field
This implements JsonForm::makeOptional and JsonForm::makeRequired string Form::checkboxList ( array $checkboxes , array $attrs );
Returns HTML for a list of checkboxes, applying name conversions along the way
Uses Fb::checkboxBoolList to generate the underlying checkbox list array JsonForm::collateData ( array $conf , string $mode , Validator $validator , int $item_id );
Collates POST data using specified config options void JsonForm::collateFieldData ( array $field_defn , string $input , array $metadata , Validator $valid , array &$data );
Collates a single field's $_POST data for INSERT/UPDATE queries, and performs validation string Form::convertFieldName ( string $name );
Format a field name as per the specification defined by Form::setFieldNameFormat void JsonForm::expandItemDefns ( array &$field , array $metadata );
Expands item definitions for a field pulled from JSON string Form::fieldAuto ( callable $method , string $name , array $attrs , array $options );
Return HTML for a field, with the wrapping HTML detected automatically.
To enable fieldset wrapping, add the docblock tag @wrap-in-fieldset to the field generation method string Form::fieldFieldset ( callable $method , string $name , array $attrs , array $options );
Return HTML for a field wrapped in a FIELDSET
The main wrapping DIV will contain additional classes if the field is required, disabled or has an error.
A class is also output for hte field method name (if the name contains "Sprout\Helpers\Fb::" this is removed)
If the field has an explicit ID set, that will be added as a class on the wrapper too.
The special attribute "-wrapper-class" can be used to add classes to the wrapper DIV.
Multiple classes can be specified, space separated.
These classes will be prefixed with "field-element--" string Form::fieldMethodClass ( string $method );
Convert a full method name (e.g. Sprout\Helpers\Fb::text) into a friendly class name
The classes Fb and Form aren't emitted, but all other class names are string Form::fieldPlain ( callable $method , string $name , array $attrs , array $options );
Return HTML for a 'plain' field, i.e. one which doesn't require a FIELDSET wrapped around it.
The main wrapping DIV will contain additional classes if the field is required, disabled or has an error.
A class is also output for the field method name (if the name contains "Sprout\Helpers\Fb::" this is removed)
If the field has an explicit ID set, that will be added as a class on the wrapper too.
The special attribute "-wrapper-class" can be used to add classes to the wrapper DIV.
Multiple classes can be specified, space separated.
These classes will be prefixed with "field-element--" array JsonForm::flattenGroups ( array $items );
Extract field defns from a list (which may include groups) string Form::genId ( );
Generate a unique id which should be stable across calls to this URL
as long as the number and order of fields on the page remains the same unknown Form::getData ( string $field );
Gets the form per-field value for a single field
As form field datas are stored using the Fb class, this method just gets the data from there NULL|array Form::getFieldErrors ( string $field_name );
Return the errors for a given field
Supports nested error arrays; If $field_name is something like member[5][test] then the error
will be read from self::$errors['member']['5']['test'] string Form::html ( string $html );
Return HTML which has been wrapped in the form field DIVs array JsonForm::loadAutofillListData ( array $conf , string $local_table_name , int $local_record_id , array $conditions );
Loads autofill_list data for use on a view null|array Form::loadFromSession ( string $key [, mixed $verify_record_id ] );
Load data and errors from the session, with optional record id validation
Expected session keys:
record_id Checked against $verify_record_id, session data is thrown away in case of mismatch
field_values Field data, loaded using Form::setData
field_errors Field errors, loaded using Form::setErrors array JsonForm::loadMultiEditData ( array $conf , string $default_link , int $record_id , array $conditions );
Loads multiedit data for use on a view void JsonForm::makeOptional ( array &$conf , string $field_name );
Modify a JSON form config to make a particular field optional void JsonForm::makeRequired ( array &$conf , string $field_name );
Modify a JSON form config to make a particular field required string Form::money ( string $name , array $attrs , array $options );
Returns HTML for a money field, using Fb::money to generate the field itself string Form::multiradio ( string $name , array $attrs , array $options );
Returns HTML for a bunch of radiobuttons, using Fb::multiradio to generate the fields void Form::nextFieldDetails ( string $label , bool $required [, string $helptext ] );
Set the details for the next field which will be outputted.
After returning a field, these values will be cleared from the state machine
Both the label and helptext support a subset of HTML, Text::limitedSubsetHtml for more details string Form::number ( string $name , array $attrs );
Returns HTML for a number field, using Fb::number to generate the field itself public out (from Form)string Form::out ( string $plain );
Return content which has been HTML-encoded and wrapped in the form field DIVs string Form::passString ( string $str );
Returns the first argument
This hacky little method works around the fact that fieldPlain only accepts a method name string Form::password ( string $name , array $attrs );
Returns HTML for a password field, using Fb::password to generate the field itself string JsonForm::renderField ( array $field [, string $name_prepend , array $metadata ] );
Renders the input for a field definition pulled from a JSON file html JsonForm::renderTabItem ( array $item , string $for , int $id , array $data , array $errors [, string $name_prepend ] );
Render a tab item, which may be a field, heading, html block, etc void Form::resetField ( );
Reset the state machine for field values void Form::setData ( array $data );
Set form per-field values for the fields
As form fields are rendered using the Fb class, this method just sets the data there void Form::setErrors ( array $errors );
Set per-field error messages to display
A given field can have either a single error message or an array of errors
The output from the Validator::getFieldErrors method can be used directly as input to this method void Form::setFieldIdPrefix ( string $prefix );
Sets the prefix for generated IDs void Form::setFieldNameFormat ( string $format );
Set a format string which will alter the field name prior to being passed to the underlying render method
Formatting is done using sprintf
A single parameter is provided to the sprintf() call, the field name
The default format does no transformation, i.e. the string '%s'
This parameter persists across multiple form fields void Form::setFieldValue ( array $field , array $value );
Sets the value for a single field
As form fields are rendered using the Fb class, this method just sets the data there null JsonForm::setParameterForColumns ( array &$items , array $columns , string $key , string $val );
Set a parameter for fields to be a specific value, for one or more columns string Form::text ( string $name , array $attrs );
Returns HTML for a text field, using Fb::text to generate the field itself
|