PHP Class Symfony\Component\Form\Field

To implement your own form fields, you need to have a thorough understanding of the data flow within a form field. A form field stores its data in three different representations: (1) the format required by the form's object (2) a normalized format for internal processing (3) the 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 both for internal processing as for 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 setValueTransformer() in the configure() method. 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 setNormalizationTransformer() in configure(). The normalized data is then converted to the displayed data as described before.
Author: Bernhard Schussek ([email protected])
Inheritance: extends Symfony\Component\Form\Configurable, implements Symfony\Component\Form\FieldInterface
Mostra file Open project: pmjones/php-framework-benchmarks Class Usage Examples

Protected Properties

Property Type Description
$locale
$taintedData

Public Methods

Method Description
__clone ( ) Clones this field.
__construct ( $key, array $options = [] )
addError ( Symfony\Component\Form\FieldError $error, Symfony\Component\Form\PropertyPathIterator $pathIterator = null, $type = null ) Adds an error to the field.
bind ( string | array $taintedData ) : boolean Binds POST data to the field, transforms and validates it.
getData ( ) : mixed Returns the normalized data of the field.
getDisplayedData ( ) : string | array Returns the data of the field as it is displayed to the user.
getErrors ( ) : array Returns all errors
getId ( ) {@inheritDoc}
getKey ( ) {@inheritDoc}
getName ( ) {@inheritDoc}
getParent ( ) : Symfony\Component\Form\FieldInterface Returns the parent field.
getPropertyPath ( ) {@inheritDoc}
hasErrors ( ) : boolean Returns weather there are errors.
isBound ( ) : boolean Returns whether the field is bound.
isDisabled ( ) {@inheritDoc}
isHidden ( ) : boolean Returns true if the widget is hidden.
isMultipart ( ) {@inheritDoc}
isRequired ( ) {@inheritDoc}
isValid ( ) : boolean Returns whether the field is valid.
setData ( $data ) Updates the field with default data
setKey ( $key ) {@inheritDoc}
setLocale ( $locale ) Sets the locale of this field.
setParent ( Symfony\Component\Form\FieldInterface $parent = null ) {@inheritDoc}
setPropertyPath ( $propertyPath ) {@inheritDoc}
setRequired ( $required ) {@inheritDoc}
updateFromObject ( &$objectOrArray ) {@inheritDoc}
updateObject ( &$objectOrArray ) {@inheritDoc}

Protected Methods

Method Description
denormalize ( string $value ) : mixed Reverse transforms a value if a normalization transformer is set.
getNormalizationTransformer ( ) : Symfony\Component\Form\ValueTransformer\ValueTransformerInterface Returns the ValueTransformer.
getNormalizedData ( )
getTransformedData ( ) : string Returns the data transformed by the value transformer
getValueTransformer ( ) : Symfony\Component\Form\ValueTransformer\ValueTransformerInterface Returns the ValueTransformer.
injectLocale ( object $object ) Injects the locale into the given object, if set.
normalize ( mixed $value ) : string Normalizes the value if a normalization transformer is set
processData ( mixed $data ) : mixed Processes the bound reverse-transformed data.
reverseTransform ( string $value ) : mixed Reverse transforms a value if a value transformer is set.
setNormalizationTransformer ( Symfony\Component\Form\ValueTransformer\ValueTransformerInterface $normalizationTransformer ) Sets the ValueTransformer.
setValueTransformer ( Symfony\Component\Form\ValueTransformer\ValueTransformerInterface $valueTransformer ) Sets the ValueTransformer.
transform ( mixed $value ) : string Transforms the value if a value transformer is set.

Method Details

__clone() public method

Clones this field.
public __clone ( )

__construct() public method

public __construct ( $key, array $options = [] )
$options array

addError() public method

Adds an error to the field.
See also: FieldInterface
public addError ( Symfony\Component\Form\FieldError $error, Symfony\Component\Form\PropertyPathIterator $pathIterator = null, $type = null )
$error Symfony\Component\Form\FieldError
$pathIterator Symfony\Component\Form\PropertyPathIterator

bind() public method

Binds POST data to the field, transforms and validates it.
public bind ( string | array $taintedData ) : boolean
$taintedData string | array The POST data
return boolean Whether the form is valid

denormalize() protected method

Reverse transforms a value if a normalization transformer is set.
protected denormalize ( string $value ) : mixed
$value string The value to reverse transform
return mixed

getData() public method

Returns the normalized data of the field.
public getData ( ) : mixed
return mixed When the field is not bound, the default data is returned. When the field is bound, the normalized bound data is returned if the field is valid, null otherwise.

getDisplayedData() public method

Returns the data of the field as it is displayed to the user.
public getDisplayedData ( ) : string | array
return string | array When the field is not bound, the transformed default data is returned. When the field is bound, the bound data is returned.

getErrors() public method

Returns all errors
public getErrors ( ) : array
return array An array of errors that occured during binding

getId() public method

{@inheritDoc}
public getId ( )

getKey() public method

{@inheritDoc}
public getKey ( )

getName() public method

{@inheritDoc}
public getName ( )

getNormalizationTransformer() protected method

Returns the ValueTransformer.
protected getNormalizationTransformer ( ) : Symfony\Component\Form\ValueTransformer\ValueTransformerInterface
return Symfony\Component\Form\ValueTransformer\ValueTransformerInterface

getNormalizedData() protected method

protected getNormalizedData ( )

getParent() public method

Returns the parent field.
public getParent ( ) : Symfony\Component\Form\FieldInterface
return Symfony\Component\Form\FieldInterface The parent field

getPropertyPath() public method

{@inheritDoc}
public getPropertyPath ( )

getTransformedData() protected method

Returns the data transformed by the value transformer
protected getTransformedData ( ) : string
return string

getValueTransformer() protected method

Returns the ValueTransformer.
protected getValueTransformer ( ) : Symfony\Component\Form\ValueTransformer\ValueTransformerInterface
return Symfony\Component\Form\ValueTransformer\ValueTransformerInterface

hasErrors() public method

Returns weather there are errors.
public hasErrors ( ) : boolean
return boolean true if form is bound and not valid

injectLocale() protected method

The locale is injected only if the object implements Localizable.
protected injectLocale ( object $object )
$object object

isBound() public method

Returns whether the field is bound.
public isBound ( ) : boolean
return boolean true if the form is bound to input values, false otherwise

isDisabled() public method

{@inheritDoc}
public isDisabled ( )

isHidden() public method

Returns true if the widget is hidden.
public isHidden ( ) : boolean
return boolean true if the widget is hidden, false otherwise

isMultipart() public method

{@inheritDoc}
public isMultipart ( )

isRequired() public method

{@inheritDoc}
public isRequired ( )

isValid() public method

Returns whether the field is valid.
public isValid ( ) : boolean
return boolean

normalize() protected method

Normalizes the value if a normalization transformer is set
protected normalize ( mixed $value ) : string
$value mixed The value to transform
return string

processData() protected method

This method can be overridden if you want to modify the data entered by the user. Note that the data is already in reverse transformed format. This method will not be called if reverse transformation fails.
protected processData ( mixed $data ) : mixed
$data mixed
return mixed

reverseTransform() protected method

Reverse transforms a value if a value transformer is set.
protected reverseTransform ( string $value ) : mixed
$value string The value to reverse transform
return mixed

setData() public method

Updates the field with default data
See also: FieldInterface
public setData ( $data )

setKey() public method

{@inheritDoc}
public setKey ( $key )

setLocale() public method

Sets the locale of this field.
See also: Localizable
public setLocale ( $locale )

setNormalizationTransformer() protected method

Sets the ValueTransformer.
protected setNormalizationTransformer ( Symfony\Component\Form\ValueTransformer\ValueTransformerInterface $normalizationTransformer )
$normalizationTransformer Symfony\Component\Form\ValueTransformer\ValueTransformerInterface

setParent() public method

{@inheritDoc}
public setParent ( Symfony\Component\Form\FieldInterface $parent = null )
$parent Symfony\Component\Form\FieldInterface

setPropertyPath() public method

{@inheritDoc}
public setPropertyPath ( $propertyPath )

setRequired() public method

{@inheritDoc}
public setRequired ( $required )

setValueTransformer() protected method

Sets the ValueTransformer.
protected setValueTransformer ( Symfony\Component\Form\ValueTransformer\ValueTransformerInterface $valueTransformer )
$valueTransformer Symfony\Component\Form\ValueTransformer\ValueTransformerInterface

transform() protected method

Transforms the value if a value transformer is set.
protected transform ( mixed $value ) : string
$value mixed The value to transform
return string

updateFromObject() public method

{@inheritDoc}
public updateFromObject ( &$objectOrArray )

updateObject() public method

{@inheritDoc}
public updateObject ( &$objectOrArray )

Property Details

$locale protected_oe property

protected $locale

$taintedData protected_oe property

protected $taintedData