Model a form, which can be rendered. More...
Public Member Functions | |
__construct ($action, $method, array $renderable_children=array()) | |
Create a form with a given set of fields. | |
needs_csrf () | |
Returns whether this form's submission requires a CSRF token. | |
get_method () | |
Gets the method of the form. | |
add_field (Form_Field_Model $field) | |
Add a new field to the end of the form. | |
get_field_view (Form_Field_Model $field) | |
Get a view representing a given field in the form. | |
get_fields () | |
Get the list of fields in the form. | |
get_id () | |
get the id of this form | |
add_button (Form_Button_Model $button) | |
Add a new button to the end of the form. | |
get_button_view (Form_Button_Model $button) | |
Get a view representing a given button in the form. | |
get_buttons () | |
Get the list of buttons in the form. | |
get_view () | |
Get a view representing a given field in the form. | |
process_data (array $raw_data) | |
Process and validate raw input data to the form. | |
get_value ($fieldname, $default=null) | |
Get the value of a field, formatted as result of process_data(). | |
set_values (array $values) | |
Set the default values for the form. | |
set_optional (array $fieldnames) | |
Set which fields are optional. | |
get_optional () | |
Returns an array of the fields that are optional. | |
is_field_required (Form_Field_Model $field) | |
Returns whether a Form_Field_Model is required within this form. |
Model a form, which can be rendered.
Form_Model::__construct | ( | $ | action, | |
$ | method, | |||
array $ | renderable_children = array() | |||
) |
Create a form with a given set of fields.
$action | string | |
$method | string POST, PUT, PATCH, GET The HTTP method to use when submitting the form | |
$renderable_children | array of From_Field_Model|Form_Button_Model |
Form_Model::add_button | ( | Form_Button_Model $ | button | ) |
Add a new button to the end of the form.
$button | Form_Button_Model |
Form_Model::add_field | ( | Form_Field_Model $ | field | ) |
Add a new field to the end of the form.
$field | Form_Field_Model |
Form_Model::get_button_view | ( | Form_Button_Model $ | button | ) |
Get a view representing a given button in the form.
If no button is specified, a view for the entire form is returned
$button | Form_Button_Model |
Form_Model::get_buttons | ( | ) |
Get the list of buttons in the form.
Form_Model::get_field_view | ( | Form_Field_Model $ | field | ) |
Get a view representing a given field in the form.
If no field is specified, a view for the entire form is returned
$field | Form_Field_Model |
Form_Model::get_fields | ( | ) |
Get the list of fields in the form.
Form_Model::get_id | ( | ) |
get the id of this form
Form_Model::get_method | ( | ) |
Gets the method of the form.
Form_Model::get_optional | ( | ) |
Returns an array of the fields that are optional.
Form_Model::get_value | ( | $ | fieldname, | |
$ | default = null | |||
) |
Get the value of a field, formatted as result of process_data().
It should be possible to recreate a form state as:
$form_b->set_values( $form_a->process_data($rawdata) );
$fieldname | string | |
$default | mixed = null |
Form_Model::get_view | ( | ) |
Get a view representing a given field in the form.
If no field is specified, a view for the entire form is returned
Form_Model::is_field_required | ( | Form_Field_Model $ | field | ) |
Returns whether a Form_Field_Model is required within this form.
$field | Form_Field_Model |
Form_Model::needs_csrf | ( | ) |
Returns whether this form's submission requires a CSRF token.
Form_Model::process_data | ( | array $ | raw_data | ) |
Process and validate raw input data to the form.
Map the raw data from for example $_POST (as input prameter) to a validated and processed set of elements.
Elements not related to the form are not returned (might also be fields depending on other fields that are hidden)
The result might also be processed to match a given format. For example, an object selector which takes a unique key string as raw argument can return an actual object instance.
$raw_data | array of parameters fetched as $_POST |
Form_Model::set_optional | ( | array $ | fieldnames | ) |
Set which fields are optional.
Optional fields are handle in such a way that if the process_data function of that field throws a MissingValueException, that exception is ignored.
Optional fields that throw MissingValueException are NOT part of the array result of the form->process_data call.
$fieldnames | array |
Form_Model::set_values | ( | array $ | values | ) |
Set the default values for the form.
$values | array |