PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Symfony Form: Extract Values

Note: This article was originally published at Planet PHP on 19 April 8000.
Planet PHP

I had to look deep into Symfony 1.4 code this morning as I was trying to get a field's value. Form values come from different sources: default values, record values (when editing, for example), original POST values and clean POST values. So when is each available and how do we get it?

Get the values by type

Default values are what you will be presented with when you display an empty form.

Get all values using $form-getDefaults() or a single value using $form-getDefault($name).

Record values, whichAare merged with the default values, are displayed when a Doctrine record is associated with the form.

Get all values using $form-getDefaults() or a single value using $form-getDefault($name).

Original POST values are displayed when the form validation failed.

Get all values using $form-getTaintedValues().

Clean POST values are the ones that have been validated and transformed by the form validators. At that point,Ayou will most likely choose to save the form and redirect.

Get all values using $form-getValues() or a single value using $form-getValue($name).

Get all values regardless of origin

In one scenario, I needed to access the form's values to inject them into Javascript. There is a nice but not well documented method to extract a value as it will appear in the html, without worrying about whether it's default, record, dirty APOST or clean POST.

Get the value directly from the field using $form-offsetGet($name)-getValue().