8000 [2.3] [Form] Renamed option "virtual" to "inherit_data" and improved handling of such forms by webmozart · Pull Request #6573 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.3] [Form] Renamed option "virtual" to "inherit_data" and improved handling of such forms #6573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 19, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[Form] Renamed option "virtual" to "inherit_data"
  • Loading branch information
webmozart committed Apr 19, 2013
commit 8ea5e1a678ef770bfb4f5ba304fcd194c5856537
75 changes: 74 additions & 1 deletion UPGRADE-2.3.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
UPGRADE FROM 2.2 to 2.3
UPGRADE FROM 2.2 to 2.3
=======================

### Form
Expand Down Expand Up @@ -35,6 +35,79 @@ UPGRADE FROM 2.2 to 2.3
"validation_groups" => false
"validation_groups" => array()
```
* The array type hint from DataMapperInterface was removed. You should adapt
implementations of that interface accordingly.

Before:

```
use Symfony\Component\Form\DataMapperInterface;

class MyDataMapper
{
public function mapFormsToData(array $forms, $data)
{
// ...
}

public function mapDataToForms($data, array $forms)
{
// ...
}
}
```

After:

```
use Symfony\Component\Form\DataMapperInterface;

class MyDataMapper
{
public function mapFormsToData($forms, $data)
{
// ...
}

public function mapDataToForms($data, $forms)
{
// ...
}
}
```

Instead of an array, the methods here are now passed a
RecursiveIteratorIterator containing an InheritDataAwareIterator by default,
so you don't need to handle forms inheriting their parent data (former
"virtual forms") in the data mapper anymore.

Before:

```
use Symfony\Component\Form\Util\VirtualFormAwareIterator;

public function mapFormsToData(array $forms, $data)
{
$iterator = new \RecursiveIteratorIterator(
new VirtualFormAwareIterator($forms)
);

foreach ($iterator as $form) {
// ...
}
}
```

After:

```
public function mapFormsToData($forms, $data)
{
foreach ($forms as $form) {
// ...
}
}
```

### PropertyAccess

Expand Down
36 changes: 36 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,42 @@ UPGRADE FROM 2.x to 3.0
}
```

* The option "virtual" was renamed to "inherit_data".

Before:

```
$builder->add('address', 'form', array(
'virtual' => true,
));
```

After:

```
$builder->add('address', 'form', array(
'inherit_data' => true,
));
```

* The class VirtualFormAwareIterator was renamed to InheritDataAwareIterator.

Before:

```
use Symfony\Component\Form\Util\VirtualFormAwareIterator;

$iterator = new VirtualFormAwareIterator($forms);
```

After:

```
use Symfony\Component\Form\Util\InheritDataAwareIterator;

$iterator = new InheritDataAwareIterator($forms);
```

### FrameworkBundle

* The `enctype` method of the `form` helper was removed. You should use the
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/Form/ButtonBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,18 @@ public function setFormProcessor(FormProcessorInterface $formProcessor)
throw new \BadMethodCallException('Buttons do not support form processors.');
}

/**
* Unsupported method.
*
* @param Boolean $inheritData
*
* @throws \BadMethodCallException
*/
public function setInheritData($inheritData)
{
throw new \BadMethodCallException('Buttons do not support data inheritance.');
}

/**
* Builds and returns the button configuration.
*
Expand Down Expand Up @@ -759,6 +771,16 @@ public function getFormProcessor()
return null;
}

/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getInheritData()
{
return null;
}

/**
* Returns all options passed during the construction of the button.
*
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* added FormProcessorInterface and FormInterface::process()
* deprecated passing a Request instance to FormInterface::bind()
* added options "method" and "action" to FormType
* deprecated option "virtual", renamed it to "inherit_data"

2.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function mapDataToForms($data, array $forms)
$iterator = new \RecursiveIteratorIterator($iterator);

foreach ($iterator as $form) {
/* @var FormInterface $form */
/* @var \Symfony\Component\Form\FormInterface $form */
$propertyPath = $form->getPropertyPath();
$config = $form->getConfig();

Expand All @@ -83,7 +83,7 @@ public function mapFormsToData(array $forms, &$data)
$iterator = new \RecursiveIteratorIterator($iterator);

foreach ($iterator as $form) {
/* @var FormInterface $form */
/* @var \Symfony\Component\Form\FormInterface $form */
$propertyPath = $form->getPropertyPath();
$config = $form->getConfig();

Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->setPropertyPath($options['property_path'])
->setMapped($options['mapped'])
->setByReference($options['by_reference'])
->setVirtual($options['virtual'])
->setInheritData($options['inherit_data'])
->setCompound($options['compound'])
->setData(isset($options['data']) ? $options['data'] : null)
->setDataLocked(isset($options['data']))
Expand Down Expand Up @@ -95,8 +95,8 @@ public function buildView(FormView $view, FormInterface $form, array $options)
'size' => null,
'label_attr' => $options['label_attr'],
'compound' => $form->getConfig()->getCompound(),
'method' => $form->getConfig()->getMethod(),
'action' => $form->getConfig()->getAction(),
'method' => $form->getConfig()->getMethod(),
'action' => $form->getConfig()->getAction(),
));
}

Expand Down Expand Up @@ -169,7 +169,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'by_reference' => true,
'error_bubbling' => $errorBubbling,
'label_attr' => array(),
'virtual' => false,
'inherit_data' => false,
'compound' => true,
'method' => 'POST',
// According to RFC 2396 (http://www.ietf.org/rfc/rfc2396.txt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public function mapViolation(ConstraintViolation $violation, FormInterface $form
}

// This case happens if an error happened in the data under a
// virtual form that does not match any of the children of
// the virtual form.
// form inheriting its parent data that does not match any of the
// children of that form.
if (null !== $violationPath && !$match) {
// If we could not map the error to anything more specific
// than the root element, map it to the innermost directly
Expand Down Expand Up @@ -162,7 +162,7 @@ private function matchChild(FormInterface $form, PropertyPathIteratorInterface $
}
}

// Ignore virtual forms when iterating the children
// Skip forms inheriting their parent data when iterating the children
$childIterator = new \RecursiveIteratorIterator(
new VirtualFormAwareIterator($form->all())
);
Expand Down Expand Up @@ -253,8 +253,8 @@ private function reconstructPath(ViolationPath $violationPath, FormInterface $or
// Process child form
$scope = $scope->get($it->current());

if ($scope->getConfig()->getVirtual()) {
// Form is virtual
if ($scope->getConfig()->getInheritData()) {
// Form inherits its parent data
// Cut the piece out of the property path and proceed
$propertyPathBuilder->remove($i);
} elseif (!$scope->getConfig()->getMapped()) {
Expand Down
40 changes: 36 additions & 4 deletions src/Symfony/Component/Form/FormConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
/**
* @var Boolean
*/
private $virtual = false;
private $inheritData = false;

/**
* @var Boolean
Expand Down Expand Up @@ -341,9 +341,24 @@ public function getByReference()
/**
* {@inheritdoc}
*/
public function getInheritData()
{
return $this->inheritData;
}

/**
* Alias of {@link getInheritData()}.
*
* @return FormConfigBuilder The configuration object.
*
* @deprecated Deprecated since version 2.2, to be removed in 2.3. Use
* {@link getInheritData()} instead.
*/
public function getVirtual()
{
return $this->virtual;
trigger_error('getVirtual() is deprecated since version 2.2 and will be removed in 2.3. Use getInheritData() instead.', E_USER_DEPRECATED);

return $this->getInheritData();
}

/**
Expand Down Expand Up @@ -676,17 +691,34 @@ public function setByReference($byReference)
/**
* {@inheritdoc}
*/
public function setVirtual($virtual)
public function setInheritData($inheritData)
{
if ($this->locked) {
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}

$this->virtual = $virtual;
$this->inheritData = $inheritData;

return $this;
}

/**
* Alias of {@link setInheritData()}.
*
* @param Boolean $inheritData Whether the form should inherit its parent's data.
*
* @return FormConfigBuilder The configuration object.
*
* @deprecated Deprecated since version 2.2, to be removed in 2.3. Use
* {@link setInheritData()} instead.
*/
public function setVirtual($inheritData)
{
trigger_error('setVirtual() is deprecated since version 2.2 and will be removed in 2.3. Use setInheritData() instead.', E_USER_DEPRECATED);

$this->setInheritData($inheritData);
}

/**
* {@inheritdoc}
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Form/FormConfigBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ public function setMapped($mapped);
public function setByReference($byReference);

/**
* Sets whether the form should be virtual.
* Sets whether the form should read and write the data of its parent.
*
* @param Boolean $virtual Whether the form should be virtual.
* @param Boolean $inheritData Whether the form should inherit its parent's data.
*
* @return self The configuration object.
*/
public function setVirtual($virtual);
public function setInheritData($inheritData);

/**
* Sets whether the form should be compound.
Expand Down
10 changes: 3 additions & 7 deletions src/Symfony/Component/Form/FormConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,11 @@ public function getMapped();
public function getByReference();

/**
* Returns whether the form should be virtual.
* Returns whether the form should read and write the data of its parent.
*
* When mapping data to the children of a form, the data mapper
* should ignore virtual forms and map to the children of the
* virtual form instead.
*
* @return Boolean Whether the form is virtual.
* @return Boolean Whether the form should inherit its parent's data.
*/
public function getVirtual();
public function getInheritData();

/**
* Returns whether the form is compound.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function testMapDataToFormsIgnoresEmptyData()
$this->assertNull($form->getData());
}

public function testMapDataToFormsSkipsVirtualForms()
public function testMapDataToFormsSkipsFormsInheritingParentData()
{
$car = new \stdClass();
$engine = new \stdClass();
Expand All @@ -195,7 +195,7 @@ public function testMapDataToFormsSkipsVirtualForms()

$config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
$config->setByReference(true);
$config->setVirtual(true);
$config->setInheritData(true);
$config->setCompound(true);
$config->setDataMapper($this->getDataMapper());
$form = $this->getForm($config);
Expand Down Expand Up @@ -348,7 +348,7 @@ public function testMapFormsToDataIgnoresDisabled()
$this->mapper->mapFormsToData(array($form), $car);
}

public function testMapFormsToDataSkipsVirtualForms()
public function testMapFormsToDataSkipsFormsInheritingParentData()
{
$car = new \stdClass();
$engine = new \stdClass();
Expand All @@ -366,7 +366,7 @@ public function testMapFormsToDataSkipsVirtualForms()

$config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
$config->setPropertyPath($parentPath);
$config->setVirtual(true);
$config->setInheritData(true);
$config->setCompound(true);
$config->setDataMapper($this->getDataMapper());
$form = $this->getForm($config);
Expand Down
Loading
0