AnewtFormControl class

Base form control class.

Class Overview

Base form control class.

All form controls must extend this class.

Do not instantiate this class directly, use one of its descendants instead.

Public Methods

__construct($name)

Create a new form control instance.

If a subclass overrides the AnewtFormControl constructor, the parent constructor must be invoked as well.

Parameters

$name

The name of this control. It must be unique across this form.

Getter and setter methods

fill($values)

Fill the form control from the given values.

This only sets the value for controls for which the can-be-filled property is enabled.

The return value of this function indicates whether the control was successfully filled with a value. For some control types such as checkboxes this always succeeds since both presence and absence of the value from $values are meaningful. For other controls such as text inputs this is only true if the value was actually present in the $values parameters.

Parameters

$values

An associative array from which the form control should be filled.

Return value

true if filling was succesfull, false otherwise.

See also

get_id()

Return the form id.

This method falls back to the name value if no id was explicitly provided.

_set_form($form) [private]

Make the control reference its containing form.

Parameters

$form

A reference to an AnewtForm instance.

Validation methods

is_empty()

Check whether this control is to be considered 'empty'.

This method is invoked when validating a form. When a control is optional and empty, no validators are applied to it. The base implementation just checks whether the value property is an empty string, but subclasses may do more sensible checks.

Return value

True if empty, false otherwise.

is_valid()

Check whether this form control is valid.

The default is to apply all validators added to this control. Subclasses could also override this method to do something more sensible than applying validators, since some controls don't accept validators, e.g. checkboxes.

Return value

True if valid, false otherwise.

add_validator($validator, $error_message=null)

Add a validator instance to this form control.

The (optional) error message will be shown when the validator fails to validate the form control.

Parameters

$validator

A validator instance, e.g. AnewtValidatorLength or another AnewtValidator subclass.

$error_message

An optional error message to be shown when this validator fails. Leave empty to not show an error.

Rendering methods

build_widget()

Render the minimal HTML needed for this control to work.

Form control implementations should implement this method, e.g. for a checkbox a simple input HTML element should be returned.

Return value

XHTML element with the minimal HTML markup needed for this control to function properly.

Protected Attributes

$_form [protected]

Reference to containing AnewtForm instance.

See also

$_validators [protected]

List of validators.

See also

$_validator_errors [protected]

List of validator error messages.

See also

Inheritance

Base Classes

Inherited members