class Form implements IteratorAggregate, FormInterface
Form represents a form.
To implement your own form fields, you need to have a thorough understanding
of the data flow within a form. A form stores its data in three different
representations:
(1) the "model" format required by the form's object
(2) the "normalized" format for internal processing
(3) the "view" format used for display
A date field, for example, may store a date as "Y-m-d" string (1) in the
object. To facilitate processing in the field, this value is normalized
to a DateTime object (2). In the HTML representation of your form, a
localized string (3) is presented to and modified by the user.
In most cases, format (1) and format (2) will be the same. For example,
a checkbox field uses a Boolean value for both internal processing and
storage in the object. In these cases you simply need to set a value
transformer to convert between formats (2) and (3). You can do this by
calling addViewTransformer().
In some cases though it makes sense to make format (1) configurable. To
demonstrate this, let's extend our above date field to store the value
either as "Y-m-d" string or as timestamp. Internally we still want to
use a DateTime object for processing. To convert the data from string/integer
to DateTime you can set a normalization transformer by calling
addNormTransformer(). The normalized data is then converted to the displayed
data as described before.
The conversions (1) -> (2) -> (3) use the transform methods of the transformers.
The conversions (3) -> (2) -> (1) use the reverseTransform methods of the transformers.
Methods
|
__construct(FormConfigInterface $config)
Creates a new form based on the given configuration. |
||
| __clone() | ||
| FormConfigInterface |
getConfig()
Returns the form's configuration. |
|
| string |
getName()
Returns the name by which the form is identified in forms. |
|
| PropertyPathInterface |
getPropertyPath()
Returns the property path that the form is mapped to. |
|
| Boolean |
isRequired()
Returns whether the form is required to be filled out. |
|
| Boolean |
isDisabled()
Returns whether this form is disabled. |
|
| FormInterface |
setParent(FormInterface $parent = null)
Sets the parent form. |
|
| FormInterface|null |
getParent()
Returns the parent form. |
|
| FormInterface |
getRoot()
Returns the root of the form tree. |
|
| Boolean |
isRoot()
Returns whether the field is the root of the form tree. |
|
| FormInterface |
setData(mixed $modelData)
Updates the form with default data. |
|
| mixed |
getData()
Returns the data in the format needed for the underlying object. |
|
| mixed |
getNormData()
Returns the normalized data of the field. |
|
| mixed |
getViewData()
Returns the data transformed by the value transformer. |
|
| array |
getExtraData()
Returns the extra data. |
|
| FormInterface |
initialize()
Initializes the form tree. |
|
| FormInterface |
handleRequest(mixed $request = null)
Inspects the given request and calls {@link submit()} if the form was submitted. |
|
| FormInterface |
submit(null|string|array $submittedData, Boolean $clearMissing = true)
Submits data to the form, transforms and validates it. |
|
|
bind($submittedData)
Alias of {@link submit()}. |
||
| FormInterface |
addError(FormError $error)
Adds an error to this form. |
|
| Boolean |
isSubmitted()
Returns whether the form is submitted. |
|
|
isBound()
Alias of {@link isSubmitted()}. |
||
| Boolean |
isSynchronized()
Returns whether the data in the different formats is synchronized. |
|
| Boolean |
isEmpty()
Returns whether the form is empty. |
|
| Boolean |
isValid()
Returns whether the form and all children are valid. |
|
| FormError[] |
getErrors()
Returns all errors. |
|
| string |
getErrorsAsString(integer $level)
Returns a string representation of all form errors (including children errors). |
|
| FormInterface[] |
all()
Returns all children in this group. |
|
| FormInterface |
add(FormInterface|string|integer $child, string|null $type = null, array $options = array())
Adds a child to the form. |
|
| FormInterface |
remove(string $name)
Removes a child from the form. |
|
| Boolean |
has(string $name)
Returns whether a child with the given name exists. |
|
| FormInterface |
get(string $name)
Returns the child with the given name. |
|
| Boolean |
offsetExists(string $name)
Returns whether a child with the given name exists (implements the \ArrayAccess interface). |
|
| FormInterface |
offsetGet(string $name)
Returns the child with the given name (implements the \ArrayAccess interface). |
|
|
offsetSet(string $name, FormInterface $child)
Adds a child to the form (implements the \ArrayAccess interface). |
||
|
offsetUnset(string $name)
Removes the child with the given name from the form (implements the \ArrayAccess interface). |
||
| ArrayIterator |
getIterator()
Returns the iterator for this group. |
|
| integer |
count()
Returns the number of form children (implements the \Countable interface). |
|
| FormView |
createView(FormView $parent = null)
Creates a view. |
Details
at line 151
public
__construct(FormConfigInterface $config)
Creates a new form based on the given configuration.
at line 169
public
__clone()
at line 179
public FormConfigInterface
getConfig()
Returns the form's configuration.
at line 187
public string
getName()
Returns the name by which the form is identified in forms.
at line 195
public PropertyPathInterface
getPropertyPath()
Returns the property path that the form is mapped to.
at line 221
public Boolean
isRequired()
Returns whether the form is required to be filled out.
If the form has a parent and the parent is not required, this method
will always return false. Otherwise the value set with setRequired()
is returned.
at line 233
public Boolean
isDisabled()
Returns whether this form is disabled.
The content of a disabled form is displayed, but not allowed to be
modified. The validation of modified disabled forms should fail.
Forms whose parents are disabled are considered disabled regardless of
their own state.
at line 245
public FormInterface
setParent(FormInterface $parent = null)
Sets the parent form.
at line 263
public FormInterface|null
getParent()
Returns the parent form.
at line 271
public FormInterface
getRoot()
Returns the root of the form tree.
at line 279
public Boolean
isRoot()
Returns whether the field is the root of the form tree.
at line 287
public FormInterface
setData(mixed $modelData)
Updates the form with default data.
at line 389
public mixed
getData()
Returns the data in the format needed for the underlying object.
at line 409
public mixed
getNormData()
Returns the normalized data of the field.
at line 429
public mixed
getViewData()
Returns the data transformed by the value transformer.
at line 449
public array
getExtraData()
Returns the extra data.
at line 457
public FormInterface
initialize()
Initializes the form tree.
Should be called on the root form after constructing the tree.
at line 476
public FormInterface
handleRequest(mixed $request = null)
Inspects the given request and calls {@link submit()} if the form was submitted.
Internally, the request is forwarded to the configured
{@link RequestHandlerInterface} instance, which determines whether to
submit the form or not.
at line 486
public FormInterface
submit(null|string|array $submittedData, Boolean $clearMissing = true)
Submits data to the form, transforms and validates it.
at line 636
public
bind($submittedData)
Alias of {@link submit()}.
at line 644
public FormInterface
addError(FormError $error)
Adds an error to this form.
at line 658
public Boolean
isSubmitted()
Returns whether the form is submitted.
at line 669
public
isBound()
Alias of {@link isSubmitted()}.
at line 677
public Boolean
isSynchronized()
Returns whether the data in the different formats is synchronized.
at line 685
public Boolean
isEmpty()
Returns whether the form is empty.
at line 703
public Boolean
isValid()
Returns whether the form and all children are valid.
If the form is not submitted, this method always returns false.
at line 727
public FormError[]
getErrors()
Returns all errors.
at line 741
public string
getErrorsAsString(integer $level)
Returns a string representation of all form errors (including children errors).
This method should only be used to help debug a form.
at line 763
public FormInterface[]
all()
Returns all children in this group.
at line 771
public FormInterface
add(FormInterface|string|integer $child, string|null $type = null, array $options = array())
Adds a child to the form.
at line 845
public FormInterface
remove(string $name)
Removes a child from the form.
at line 863
public Boolean
has(string $name)
Returns whether a child with the given name exists.
at line 871
public FormInterface
get(string $name)
Returns the child with the given name.
at line 887
public Boolean
offsetExists(string $name)
Returns whether a child with the given name exists (implements the \ArrayAccess interface).
at line 901
public FormInterface
offsetGet(string $name)
Returns the child with the given name (implements the \ArrayAccess interface).
at line 917
public
offsetSet(string $name, FormInterface $child)
Adds a child to the form (implements the \ArrayAccess interface).
at line 929
public
offsetUnset(string $name)
Removes the child with the given name from the form (implements the \ArrayAccess interface).
at line 939
public ArrayIterator
getIterator()
Returns the iterator for this group.
at line 949
public integer
count()
Returns the number of form children (implements the \Countable interface).
at line 957
public FormView
createView(FormView $parent = null)
Creates a view.