Symfony2 API
Class

Symfony\Component\Form\FormBuilder

class FormBuilder extends FormConfigBuilder implements IteratorAggregate, FormBuilderInterface

A builder for creating {@link Form} instances.

Methods

__construct(string $name, string $dataClass, EventDispatcherInterface $dispatcher, FormFactoryInterface $factory, array $options = array())

Creates a new form builder.

FormConfigBuilderInterface addEventListener(string $eventName, callable $listener, int $priority)

Adds an event listener to an event on this form.

from FormConfigBuilder
FormConfigBuilderInterface addEventSubscriber(EventSubscriberInterface $subscriber)

Adds an event subscriber for events on this form.

from FormConfigBuilder
FormConfigBuilderInterface addViewTransformer(DataTransformerInterface $viewTransformer, bool $forcePrepend = false)

Appends / prepends a transformer to the view transformer chain.

from FormConfigBuilder
FormConfigBuilderInterface resetViewTransformers()

Clears the view transformers.

from FormConfigBuilder
FormConfigBuilderInterface addModelTransformer(DataTransformerInterface $modelTransformer, bool $forceAppend = false)

Prepends / appends a transformer to the normalization transformer chain.

from FormConfigBuilder
FormConfigBuilderInterface resetModelTransformers()

Clears the normalization transformers.

from FormConfigBuilder
EventDispatcherInterface getEventDispatcher()

Returns the event dispatcher used to dispatch form events.

from FormConfigBuilder
string getName()

Returns the name of the form used as HTTP parameter.

from FormConfigBuilder
null|PropertyPathInterface getPropertyPath()

Returns the property path that the form should be mapped to.

from FormConfigBuilder
bool getMapped()

Returns whether the form should be mapped to an element of its parent's data.

from FormConfigBuilder
bool getByReference()

Returns whether the form's data should be modified by reference.

from FormConfigBuilder
bool getInheritData()

Returns whether the form should read and write the data of its parent.

from FormConfigBuilder
FormConfigBuilder getVirtual()

Alias of {@link getInheritData()}.

from FormConfigBuilder
bool getCompound()

Returns whether the form is compound.

from FormConfigBuilder
ResolvedFormTypeInterface getType()

Returns the form types used to construct the form.

from FormConfigBuilder
DataTransformerInterface[] getViewTransformers()

Returns the view transformers of the form.

from FormConfigBuilder
DataTransformerInterface[] getModelTransformers()

Returns the model transformers of the form.

from FormConfigBuilder
DataMapperInterface getDataMapper()

Returns the data mapper of the form.

from FormConfigBuilder
bool getRequired()

Returns whether the form is required.

from FormConfigBuilder
bool getDisabled()

Returns whether the form is disabled.

from FormConfigBuilder
bool getErrorBubbling()

Returns whether errors attached to the form will bubble to its parent.

from FormConfigBuilder
mixed getEmptyData()

Returns the data that should be returned when the form is empty.

from FormConfigBuilder
array getAttributes()

Returns additional attributes of the form.

from FormConfigBuilder
bool hasAttribute(string $name)

Returns whether the attribute with the given name exists.

from FormConfigBuilder
mixed getAttribute(string $name, mixed $default = null)

Returns the value of the given attribute.

from FormConfigBuilder
mixed getData()

Returns the initial data of the form.

from FormConfigBuilder
string getDataClass()

Returns the class of the form data or null if the data is scalar or an array.

from FormConfigBuilder
bool getDataLocked()

Returns whether the form's data is locked.

from FormConfigBuilder
FormFactoryInterface getFormFactory()

Returns the form factory used for creating new forms.

from FormConfigBuilder
string getAction()

Returns the target URL of the form.

from FormConfigBuilder
string getMethod()

Returns the HTTP method used by the form.

from FormConfigBuilder
RequestHandlerInterface getRequestHandler()

Returns the request handler used by the form.

from FormConfigBuilder
bool getAutoInitialize()

Returns whether the form should be initialized upon creation.

from FormConfigBuilder
array getOptions()

Returns all options passed during the construction of the form.

from FormConfigBuilder
bool hasOption(string $name)

Returns whether a specific option exists.

from FormConfigBuilder
mixed getOption(string $name, mixed $default = null)

Returns the value of a specific option.

from FormConfigBuilder
FormConfigBuilderInterface setAttribute(string $name, mixed $value)

Sets the value for an attribute.

from FormConfigBuilder
FormConfigBuilderInterface setAttributes(array $attributes)

Sets the attributes.

from FormConfigBuilder
FormConfigBuilderInterface setDataMapper(DataMapperInterface $dataMapper = null)

Sets the data mapper used by the form.

from FormConfigBuilder
FormConfigBuilderInterface setDisabled(bool $disabled)

Set whether the form is disabled.

from FormConfigBuilder
FormConfigBuilderInterface setEmptyData(mixed $emptyData)

Sets the data used for the client data when no value is submitted.

from FormConfigBuilder
FormConfigBuilderInterface setErrorBubbling(bool $errorBubbling)

Sets whether errors bubble up to the parent.

from FormConfigBuilder
FormConfigBuilderInterface setRequired(bool $required)

Sets whether this field is required to be filled out when submitted.

from FormConfigBuilder
FormConfigBuilderInterface setPropertyPath(null|string|PropertyPathInterface $propertyPath)

Sets the property path that the form should be mapped to.

from FormConfigBuilder
FormConfigBuilderInterface setMapped(bool $mapped)

Sets whether the form should be mapped to an element of its parent's data.

from FormConfigBuilder
FormConfigBuilderInterface setByReference(bool $byReference)

Sets whether the form's data should be modified by reference.

from FormConfigBuilder
FormConfigBuilderInterface setInheritData(bool $inheritData)

Sets whether the form should read and write the data of its parent.

from FormConfigBuilder
FormConfigBuilder setVirtual(bool $inheritData)

Alias of {@link setInheritData()}.

from FormConfigBuilder
FormConfigBuilderInterface setCompound(bool $compound)

Sets whether the form should be compound.

from FormConfigBuilder
FormConfigBuilderInterface setType(ResolvedFormTypeInterface $type)

Set the types.

from FormConfigBuilder
FormConfigBuilderInterface setData(mixed $data)

Sets the initial data of the form.

from FormConfigBuilder
FormConfigBuilderInterface setDataLocked(bool $locked)

Locks the form's data to the data passed in the configuration.

from FormConfigBuilder
setFormFactory(FormFactoryInterface $formFactory)

Sets the form factory used for creating new forms.

from FormConfigBuilder
FormConfigBuilderInterface setAction(string $action)

Sets the target URL of the form.

from FormConfigBuilder
FormConfigBuilderInterface setMethod(string $method)

Sets the HTTP method used by the form.

from FormConfigBuilder
FormConfigBuilderInterface setRequestHandler(RequestHandlerInterface $requestHandler)

Sets the request handler used by the form.

from FormConfigBuilder
FormConfigBuilderInterface setAutoInitialize(bool $initialize)

Sets whether the form should be initialized automatically.

from FormConfigBuilder
FormConfigInterface getFormConfig()

Builds and returns the form configuration.

static  validateName(string|int $name)

Validates whether the given variable is a valid form name.

from FormConfigBuilder
static bool isValidName(string $name)

Returns whether the given variable contains a valid form name.

from FormConfigBuilder
FormBuilderInterface add(string|int|FormBuilderInterface $child, string|FormTypeInterface $type = null, array $options = array())

Adds a new field to this group.

FormBuilderInterface create(string $name, string|FormTypeInterface $type = null, array $options = array())

Creates a form builder.

FormBuilderInterface get(string $name)

Returns a child by name.

FormBuilderInterface remove(string $name)

Removes the field with the given name.

bool has(string $name)

Returns whether a field with the given name exists.

array all()

Returns the children.

count()

{@inheritdoc}

Form getForm()

Creates the form.

getIterator()

{@inheritdoc}

Details

at line 49
public __construct(string $name, string $dataClass, EventDispatcherInterface $dispatcher, FormFactoryInterface $factory, array $options = array())

Creates a new form builder.

Parameters

string $name The form name
string $dataClass The class of the form's data
EventDispatcherInterface $dispatcher The event dispatcher
FormFactoryInterface $factory
array $options The form options

in FormConfigBuilder at line 208
public FormConfigBuilderInterface addEventListener(string $eventName, callable $listener, int $priority)

Adds an event listener to an event on this form.

Parameters

string $eventName The name of the event to listen to.
callable $listener The listener to execute.
int $priority The priority of the listener. Listeners with a higher priority are called before listeners with a lower priority.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 222
public FormConfigBuilderInterface addEventSubscriber(EventSubscriberInterface $subscriber)

Adds an event subscriber for events on this form.

Parameters

EventSubscriberInterface $subscriber The subscriber to attach.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 236
public FormConfigBuilderInterface addViewTransformer(DataTransformerInterface $viewTransformer, bool $forcePrepend = false)

Appends / prepends a transformer to the view transformer chain.

The transform method of the transformer is used to convert data from the normalized to the view format. The reverseTransform method of the transformer is used to convert from the view to the normalized format.

Parameters

DataTransformerInterface $viewTransformer
bool $forcePrepend if set to true, prepend instead of appending

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 254
public FormConfigBuilderInterface resetViewTransformers()

Clears the view transformers.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 268
public FormConfigBuilderInterface addModelTransformer(DataTransformerInterface $modelTransformer, bool $forceAppend = false)

Prepends / appends a transformer to the normalization transformer chain.

The transform method of the transformer is used to convert data from the model to the normalized format. The reverseTransform method of the transformer is used to convert from the normalized to the model format.

Parameters

DataTransformerInterface $modelTransformer
bool $forceAppend if set to true, append instead of prepending

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 286
public FormConfigBuilderInterface resetModelTransformers()

Clears the normalization transformers.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 300
public EventDispatcherInterface getEventDispatcher()

Returns the event dispatcher used to dispatch form events.

Return Value

EventDispatcherInterface The dispatcher.

in FormConfigBuilder at line 312
public string getName()

Returns the name of the form used as HTTP parameter.

Return Value

string The form name.

in FormConfigBuilder at line 320
public null|PropertyPathInterface getPropertyPath()

Returns the property path that the form should be mapped to.

Return Value

null|PropertyPathInterface The property path.

in FormConfigBuilder at line 328
public bool getMapped()

Returns whether the form should be mapped to an element of its parent's data.

Return Value

bool Whether the form is mapped.

in FormConfigBuilder at line 336
public bool getByReference()

Returns whether the form's data should be modified by reference.

Return Value

bool Whether to modify the form's data by reference.

in FormConfigBuilder at line 344
public bool getInheritData()

Returns whether the form should read and write the data of its parent.

Return Value

bool Whether the form should inherit its parent's data.

in FormConfigBuilder at line 357
public FormConfigBuilder getVirtual()

Alias of {@link getInheritData()}.

Return Value

FormConfigBuilder The configuration object.

in FormConfigBuilder at line 367
public bool getCompound()

Returns whether the form is compound.

This property is independent of whether the form actually has children. A form can be compound and have no children at all, like for example an empty collection form.

Return Value

bool Whether the form is compound.

in FormConfigBuilder at line 375
public ResolvedFormTypeInterface getType()

Returns the form types used to construct the form.

Return Value

ResolvedFormTypeInterface The form's type.

in FormConfigBuilder at line 383
public DataTransformerInterface[] getViewTransformers()

Returns the view transformers of the form.

Return Value

DataTransformerInterface[] An array of {@link DataTransformerInterface} instances.

in FormConfigBuilder at line 391
public DataTransformerInterface[] getModelTransformers()

Returns the model transformers of the form.

Return Value

DataTransformerInterface[] An array of {@link DataTransformerInterface} instances.

in FormConfigBuilder at line 399
public DataMapperInterface getDataMapper()

Returns the data mapper of the form.

Return Value

DataMapperInterface The data mapper.

in FormConfigBuilder at line 407
public bool getRequired()

Returns whether the form is required.

Return Value

bool Whether the form is required.

in FormConfigBuilder at line 415
public bool getDisabled()

Returns whether the form is disabled.

Return Value

bool Whether the form is disabled.

in FormConfigBuilder at line 423
public bool getErrorBubbling()

Returns whether errors attached to the form will bubble to its parent.

Return Value

bool Whether errors will bubble up.

in FormConfigBuilder at line 431
public mixed getEmptyData()

Returns the data that should be returned when the form is empty.

Return Value

mixed The data returned if the form is empty.

in FormConfigBuilder at line 439
public array getAttributes()

Returns additional attributes of the form.

Return Value

array An array of key-value combinations.

in FormConfigBuilder at line 447
public bool hasAttribute(string $name)

Returns whether the attribute with the given name exists.

Parameters

string $name The attribute name.

Return Value

bool Whether the attribute exists.

in FormConfigBuilder at line 455
public mixed getAttribute(string $name, mixed $default = null)

Returns the value of the given attribute.

Parameters

string $name The attribute name.
mixed $default The value returned if the attribute does not exist.

Return Value

mixed The attribute value.

in FormConfigBuilder at line 463
public mixed getData()

Returns the initial data of the form.

Return Value

mixed The initial form data.

in FormConfigBuilder at line 471
public string getDataClass()

Returns the class of the form data or null if the data is scalar or an array.

Return Value

string The data class or null.

in FormConfigBuilder at line 479
public bool getDataLocked()

Returns whether the form's data is locked.

A form with locked data is restricted to the data passed in this configuration. The data can only be modified then by submitting the form.

Return Value

bool Whether the data is locked.

in FormConfigBuilder at line 487
public FormFactoryInterface getFormFactory()

Returns the form factory used for creating new forms.

Return Value

FormFactoryInterface The form factory.

in FormConfigBuilder at line 495
public string getAction()

Returns the target URL of the form.

Return Value

string The target URL of the form.

in FormConfigBuilder at line 503
public string getMethod()

Returns the HTTP method used by the form.

Return Value

string The HTTP method of the form.

in FormConfigBuilder at line 511
public RequestHandlerInterface getRequestHandler()

Returns the request handler used by the form.

Return Value

RequestHandlerInterface The request handler.

in FormConfigBuilder at line 526
public bool getAutoInitialize()

Returns whether the form should be initialized upon creation.

Return Value

bool Returns true if the form should be initialized when created, false otherwise.

in FormConfigBuilder at line 534
public array getOptions()

Returns all options passed during the construction of the form.

Return Value

array The passed options.

in FormConfigBuilder at line 542
public bool hasOption(string $name)

Returns whether a specific option exists.

Parameters

string $name The option name,

Return Value

bool Whether the option exists.

in FormConfigBuilder at line 550
public mixed getOption(string $name, mixed $default = null)

Returns the value of a specific option.

Parameters

string $name The option name.
mixed $default The value returned if the option does not exist.

Return Value

mixed The option value.

in FormConfigBuilder at line 558
public FormConfigBuilderInterface setAttribute(string $name, mixed $value)

Sets the value for an attribute.

Parameters

string $name The name of the attribute
mixed $value The value of the attribute

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 572
public FormConfigBuilderInterface setAttributes(array $attributes)

Sets the attributes.

Parameters

array $attributes The attributes.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 586
public FormConfigBuilderInterface setDataMapper(DataMapperInterface $dataMapper = null)

Sets the data mapper used by the form.

Parameters

DataMapperInterface $dataMapper

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 600
public FormConfigBuilderInterface setDisabled(bool $disabled)

Set whether the form is disabled.

Parameters

bool $disabled Whether the form is disabled

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 614
public FormConfigBuilderInterface setEmptyData(mixed $emptyData)

Sets the data used for the client data when no value is submitted.

Parameters

mixed $emptyData The empty data.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 628
public FormConfigBuilderInterface setErrorBubbling(bool $errorBubbling)

Sets whether errors bubble up to the parent.

Parameters

bool $errorBubbling

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 642
public FormConfigBuilderInterface setRequired(bool $required)

Sets whether this field is required to be filled out when submitted.

Parameters

bool $required

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 656
public FormConfigBuilderInterface setPropertyPath(null|string|PropertyPathInterface $propertyPath)

Sets the property path that the form should be mapped to.

Parameters

null|string|PropertyPathInterface $propertyPath The property path or null if the path should be set automatically based on the form's name.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 674
public FormConfigBuilderInterface setMapped(bool $mapped)

Sets whether the form should be mapped to an element of its parent's data.

Parameters

bool $mapped Whether the form should be mapped.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 688
public FormConfigBuilderInterface setByReference(bool $byReference)

Sets whether the form's data should be modified by reference.

Parameters

bool $byReference Whether the data should be modified by reference.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 702
public FormConfigBuilderInterface setInheritData(bool $inheritData)

Sets whether the form should read and write the data of its parent.

Parameters

bool $inheritData Whether the form should inherit its parent's data.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 723
public FormConfigBuilder setVirtual(bool $inheritData)

Alias of {@link setInheritData()}.

Parameters

bool $inheritData Whether the form should inherit its parent's data.

Return Value

FormConfigBuilder The configuration object.

in FormConfigBuilder at line 733
public FormConfigBuilderInterface setCompound(bool $compound)

Sets whether the form should be compound.

Parameters

bool $compound Whether the form should be compound.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 747
public FormConfigBuilderInterface setType(ResolvedFormTypeInterface $type)

Set the types.

Parameters

ResolvedFormTypeInterface $type The type of the form.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 761
public FormConfigBuilderInterface setData(mixed $data)

Sets the initial data of the form.

Parameters

mixed $data The data of the form in application format.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 775
public FormConfigBuilderInterface setDataLocked(bool $locked)

Locks the form's data to the data passed in the configuration.

A form with locked data is restricted to the data passed in this configuration. The data can only be modified then by submitting the form.

Parameters

bool $locked Whether to lock the default data.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 789
public setFormFactory(FormFactoryInterface $formFactory)

Sets the form factory used for creating new forms.

Parameters

FormFactoryInterface $formFactory The form factory.

in FormConfigBuilder at line 803
public FormConfigBuilderInterface setAction(string $action)

Sets the target URL of the form.

Parameters

string $action The target URL of the form.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 817
public FormConfigBuilderInterface setMethod(string $method)

Sets the HTTP method used by the form.

Parameters

string $method The HTTP method of the form.

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 841
public FormConfigBuilderInterface setRequestHandler(RequestHandlerInterface $requestHandler)

Sets the request handler used by the form.

Parameters

RequestHandlerInterface $requestHandler

Return Value

FormConfigBuilderInterface The configuration object.

in FormConfigBuilder at line 855
public FormConfigBuilderInterface setAutoInitialize(bool $initialize)

Sets whether the form should be initialized automatically.

Should be set to true only for root forms.

Parameters

bool $initialize True to initialize the form automatically, false to suppress automatic initialization. In the second case, you need to call {@link FormInterface::initialize()} manually.

Return Value

FormConfigBuilderInterface The configuration object.

at line 196
public FormConfigInterface getFormConfig()

Builds and returns the form configuration.

Return Value

FormConfigInterface

in FormConfigBuilder at line 886
static public validateName(string|int $name)

Validates whether the given variable is a valid form name.

Parameters

string|int $name The tested form name.

Exceptions

UnexpectedTypeException If the name is not a string or an integer.
InvalidArgumentException If the name contains invalid characters.

in FormConfigBuilder at line 914
static public bool isValidName(string $name)

Returns whether the given variable contains a valid form name.

A name is accepted if it

Parameters

string $name The tested form name.

Return Value

bool Whether the name is valid.

at line 59
public FormBuilderInterface add(string|int|FormBuilderInterface $child, string|FormTypeInterface $type = null, array $options = array())

Adds a new field to this group.

A field must have a unique name within the group. Otherwise the existing field is overwritten.

If you add a nested group, this group should also be represented in the object hierarchy.

Parameters

string|int|FormBuilderInterface $child
string|FormTypeInterface $type
array $options

Return Value

FormBuilderInterface The builder object.

at line 95
public FormBuilderInterface create(string $name, string|FormTypeInterface $type = null, array $options = array())

Creates a form builder.

Parameters

string $name The name of the form or the name of the property
string|FormTypeInterface $type The type of the form or null if name is a property
array $options The options

Return Value

FormBuilderInterface The created builder.

at line 115
public FormBuilderInterface get(string $name)

Returns a child by name.

Parameters

string $name The name of the child

Return Value

FormBuilderInterface The builder for the child

Exceptions

InvalidArgumentException if the given child does not exist

at line 135
public FormBuilderInterface remove(string $name)

Removes the field with the given name.

Parameters

string $name

Return Value

FormBuilderInterface The builder object.

at line 150
public bool has(string $name)

Returns whether a field with the given name exists.

Parameters

string $name

Return Value

bool

at line 170
public array all()

Returns the children.

Return Value

array

at line 184
public count()

{@inheritdoc}

at line 210
public Form getForm()

Creates the form.

Return Value

Form The form

at line 236
public getIterator()

{@inheritdoc}