Basic form class.
Basic form class.
This class can be used to create an XHTML form. An AnewtForm instance holds a number of controls, fieldset or custom XHTML elements. Additionally, it has a few properties that influence its behaviour:
id property is the form idmethod is either ANEWT_FORM_METHOD_GET or ANEWT_FORM_METHOD_POST action is the URL the form will be posted toUsually an AnewtForm subclass is created. The constructor of this subclass adds controls to the form, and optionally the handle_valid() and handle_invalid() methods are overridden. Calling code then instantiates this subclass, fills it with values using fill() or autofill(), then processes the form using process().
Reference module documentation once it's written.
__construct() ¶Initialize a new AnewtForm instance.
Do not forget to call this method when you override the constructor in a subclass, i.e. call parent::__construct().
setup($id=null, $method=null, $action=null) ¶Setup the form in one step.
This is just a convenience method, any of the arguments are optional.
$idId of the form
$methodForm method; must be one of the ANEWT_FORM_METHOD_GET or ANEWT_FORM_METHOD_POST constants
$actionThe url to post this form to
get_method_as_string() ¶Return the form method as a string.
Form method as string, either GET or POST.
fill($values) ¶Fill form using the supplied values.
This will iterate over all controls of the form and set their value accordingly (if any).
The return value can be used to determine whether a form was completely filled using the provided data, or that some values were missing from the $values array. See autofill() for more information about this.
$valuesAssociative array used as a name to value mapping.
true if the form was succesfully and completely using the provided $values, false otherwise.
autofill() ¶Fill form automatically from query data.
If the form uses the GET method the data comes from $_GET. If the form uses the POST method the data comes from $_POST.
The return value (see also fill() for an explanation) can be used to detect incomplete $_GET or $_POST values. This may happen when handling different form flavours with different controls using a single form, e.g. a simple and advanced search form pointing to the same page in which an instance of the advanced flavour of the form handles the submitted data. It may also happen when users are messing with the submitted data. You may then decide to process() the form only if all values were provided. Example: if ($form->autofill() && $form->process()) { ...} else { ...}
True if this fill could be automatically filled from $_GET or $_POST, false if this was not the case.
get_control_value($name) ¶Get the value of a control.
The specified control must exist.
This method takes into account whether a control is empty and optional, in which case NULL is returned. For this reason, the value as returned by this method may differ from the value obtained using get('value') on the control instance itself, which always returns the ‘real’ control value. (This is because you can change the required property afterwards, so trying to be smart is not what we want there.)
$nameThe name of the control
The value of the control
set_control_value($name, $value) ¶Set the value of a control.
The specified control must exist.
$nameThe name of the control
$valueThe value to set
get_control_values() ¶Retrieve all form values.
See AnewtForm::get_control_value for a description of the values returned by this method, which may contain NULL in some cases.
Associative array with all form control values by control name.
process() ¶Process this form.
This will validate the form and all controls, and call either handle_valid() or handle_invalid() depending on the validation results. The return value of the handle_valid() or handle_invalid() function is returned to the caller of this method.
If the handle_valid() and handle_invalid() methods are not overridden true and false are returned, respectively. This allows calling code to use something like this: if ($form->process()) { ... } else { ... }
Return value from handle_valid() or handle_invalid();
handle_valid() ¶Callback when form validation was succesful.
This method returns true. You may override this method in subclasses.
handle_invalid() ¶Callback when form validation did not succeed.
This method returns false. You may override this method in subclasses.
is_valid() ¶Checks whether the form is completely valid.
This iterates over all controls and checks their validity as well.
True if valid, false otherwise
add_control($control) ¶Add a control to this form.
$controlThe form control instance to add
add_fieldset($fieldset) ¶Add a fieldset to this form.
$fieldsetThe fieldset instance to add
add_node($node) ¶Add a custom node to this form.
This node is directly embedded in the rendered output by form renderers.
$nodeAn AnewtXMLDomNode to embed in the form.
has_control($name) ¶Check whether a control with this name already exists.
$nameThe name of the control
Boolean indicating Whether the control exists
get_control($name) ¶Get a reference to a control.
The control must exist for this function to work.
$nameThe name of the control
A reference to the control instance
get_fieldset($name) ¶Get a reference to a fieldset.
The fieldset must exist for this function to work.
$nameThe name of the fieldset
A reference to the AnewtFormFieldset instance
_contains_file_upload_control() ¶Check whether this form contains a file upload control (or a derived control).
This is useful to find out which enctype the form should have.
This method is intended for form renderer implementations only.
True if the form contains a file upload control, false otherwise.
Implement AnewtFormControlFileUpload
Set a special flag in add_control instead of using this method
_children() [protected] ¶Return all children on this form as a list.
This method is intended for form renderer implementations only.
List of children, e.g. form control instances and fieldsets.
$_children [private] ¶Numeric array to hold all children of this form.
$_controls_by_name [private] ¶Associative array to hold all form controls by name.
$_fieldsets_by_name [private] ¶Associative array to hold all form fieldsets by name.
AnewtContainer::_to_array()AnewtContainer::_add()AnewtContainer::delete()AnewtContainer::get()AnewtContainer::_isset()AnewtContainer::_seed()AnewtContainer::setdefault()AnewtContainer::getdefault()AnewtContainer::set()AnewtContainer::__isset()AnewtContainer::__set()AnewtContainer::__construct()AnewtContainer::is_set()AnewtContainer::add()AnewtContainer::_getdefault()AnewtContainer::__unset()AnewtContainer::seed()AnewtContainer::to_array()AnewtContainer::_get()AnewtContainer::__get()AnewtContainer::keys()AnewtContainer::clear()AnewtContainer::_keys()AnewtContainer::_set()