Skip to content

Commit

Permalink
[Form] Added option 'data' to Field for populating a field with a fix…
Browse files Browse the repository at this point in the history
…ed value
  • Loading branch information
Bernhard Schussek committed Feb 1, 2011
1 parent fdbc064 commit e5ed98c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
13 changes: 12 additions & 1 deletion src/Symfony/Component/Form/Field.php
Expand Up @@ -65,6 +65,7 @@ class Field extends Configurable implements FieldInterface

public function __construct($key = null, array $options = array())
{
$this->addOption('data');

This comment has been minimized.

Copy link
@umpirsky

umpirsky Apr 13, 2011

Contributor

Looks like label option is missing in Field cass.

Use case:

 $form->add(new TextField('subject', array(
    'max_length' => 100,
    'label' => 'Subject: *'
)));

in the template:

{{ form_field(form) }}

With current code this is not possible.

$this->addOption('trim', true);
$this->addOption('required', true);
$this->addOption('disabled', false);
Expand All @@ -74,6 +75,14 @@ public function __construct($key = null, array $options = array())

$this->key = (string)$key;

if (isset($options['data'])) {
// Populate the field with fixed data
// Set the property path to NULL so that the data is not
// overwritten by the form's data
$this->setData($options['data']);
$this->setPropertyPath(null);
}

parent::__construct($options);

if ($this->getOption('value_transformer')) {
Expand All @@ -87,7 +96,9 @@ public function __construct($key = null, array $options = array())
$this->normalizedData = $this->normalize($this->data);
$this->transformedData = $this->transform($this->normalizedData);

$this->setPropertyPath($this->getOption('property_path'));
if (!$this->getOption('data')) {
$this->setPropertyPath($this->getOption('property_path'));
}
}

/**
Expand Down
16 changes: 4 additions & 12 deletions src/Symfony/Component/Form/Form.php
Expand Up @@ -73,27 +73,19 @@ public function __construct($name = null, array $options = array())

parent::__construct($name, $options);

// If data is passed to this constructor, objects from parent forms
// should be ignored
// if (null !== $data) {
// $this->setPropertyPath(null);
// }

// Enable CSRF protection, if necessary
// TODO only in root form
if ($this->getOption('csrf_provider')) {
if (!$this->getOption('csrf_provider') instanceof CsrfProviderInterface) {
throw new FormException('The object passed to the "csrf_provider" option must implement CsrfProviderInterface');
}

$fieldName = $this->getOption('csrf_field_name');
$token = $this->getOption('csrf_provider')->generateCsrfToken(get_class($this));

$field = new HiddenField($this->getOption('csrf_field_name'), array(
'property_path' => null,
));
$field->setData($token);

$this->add($field);
$this->add(new HiddenField($fieldName, array(
'data' => $token,
)));
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/Symfony/Component/Form/FormContext.php
Expand Up @@ -130,19 +130,17 @@ public function csrfProvider(CsrfProviderInterface $csrfProvider)
*/
public function getForm($name, $data = null)
{
$form = new Form(
return new Form(
$name,
array(
'data' => $data,
'validator' => $this->validator,
'csrf_field_name' => $this->csrfFieldName,
'csrf_provider' => $this->csrfProtection ? $this->csrfProvider : null,
'validation_groups' => $this->validationGroups,
'field_factory' => $this->fieldFactory,
)
);
$form->setData($data);

return $form;
}

/**
Expand Down
19 changes: 18 additions & 1 deletion tests/Symfony/Tests/Component/Form/FieldTest.php
Expand Up @@ -19,7 +19,7 @@
use Symfony\Component\Form\ValueTransformer\ValueTransformerInterface;
use Symfony\Component\Form\PropertyPath;
use Symfony\Component\Form\FieldError;
use Symfony\Component\Form\FormContext;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
use Symfony\Tests\Component\Form\Fixtures\Author;
use Symfony\Tests\Component\Form\Fixtures\TestField;
Expand Down Expand Up @@ -489,6 +489,23 @@ public function testGetRootReturnsRootOfParentIfSet()
$this->assertEquals('ROOT', $this->field->getRoot());
}

public function testFieldsInitializedWithDataAreNotUpdatedWhenAddedToForms()
{
$author = new Author();
$author->firstName = 'Bernhard';

$field = new TestField('firstName', array(
'data' => 'foobar',
));

$form = new Form('author', array(
'data' => $author,
));
$form->add($field);

$this->assertEquals('foobar', $field->getData());
}

public function testGetRootReturnsFieldIfNoParent()
{
$this->assertEquals($this->field, $this->field->getRoot());
Expand Down

0 comments on commit e5ed98c

Please sign in to comment.