Skip to content

[Form] Fixed edge cases in MergeCollectionListener #3256

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 5 commits into from
Feb 2, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
* the collection, choice (with multiple selection) and entity (with multiple
selection) types now make use of addXxx() and removeXxx() methods in your
model
* added options "adder_prefix" and "remover_prefix" to collection and choice
type
* added options "add_method" and "remove_method" to collection and choice type

### HttpFoundation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

namespace Symfony\Component\Form\Extension\Core\EventListener;

use Symfony\Component\Form\Util\FormUtil;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Util\FormUtil;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand All @@ -42,24 +42,24 @@ class MergeCollectionListener implements EventSubscriberInterface
private $useAccessors;

/**
* The prefix of the adder method to look for
* The name of the adder method to look for
* @var string
*/
private $adderPrefix;
private $addMethod;

/**
* The prefix of the remover method to look for
* The name of the remover method to look for
* @var string
*/
private $removerPrefix;
private $removeMethod;

public function __construct($allowAdd = false, $allowDelete = false, $useAccessors = true, $adderPrefix = 'add', $removerPrefix = 'remove')
public function __construct($allowAdd = false, $allowDelete = false, $useAccessors = true, $addMethod = null, $removeMethod = null)
{
$this->allowAdd = $allowAdd;
$this->allowDelete = $allowDelete;
$this->useAccessors = $useAccessors;
$this->adderPrefix = $adderPrefix;
$this->removerPrefix = $removerPrefix;
$this->addMethod = $addMethod;
$this->removeMethod = $removeMethod;
}

static public function getSubscribedEvents()
Expand All @@ -70,11 +70,18 @@ static public function getSubscribedEvents()
public function onBindNormData(FilterDataEvent $event)
{
$originalData = $event->getForm()->getData();

// If we are not allowed to change anything, return immediately
if (!$this->allowAdd && !$this->allowDelete) {
$event->setData($originalData);
return;
}

$form = $event->getForm();
$data = $event->getData();
$parentData = $form->hasParent() ? $form->getParent()->getData() : null;
$adder = null;
$remover = null;
$addMethod = null;
$removeMethod = null;

if (null === $data) {
$data = array();
Expand All @@ -90,28 +97,59 @@ public function onBindNormData(FilterDataEvent $event)

// Check if the parent has matching methods to add/remove items
if ($this->useAccessors && is_object($parentData)) {
$plural = ucfirst($form->getName());
$singulars = (array) FormUtil::singularify($plural);
$reflClass = new \ReflectionClass($parentData);
$addMethodNeeded = $this->allowAdd && !$this->addMethod;
$removeMethodNeeded = $this->allowDelete && !$this->removeMethod;

foreach ($singulars as $singular) {
$adderName = $this->adderPrefix . $singular;
$removerName = $this->removerPrefix . $singular;
// Any of the two methods is required, but not yet known
if ($addMethodNeeded || $removeMethodNeeded) {
$singulars = (array) FormUtil::singularify(ucfirst($form->getName()));

if ($reflClass->hasMethod($adderName) && $reflClass->hasMethod($removerName)) {
$adder = $reflClass->getMethod($adderName);
$remover = $reflClass->getMethod($removerName);
foreach ($singulars as $singular) {
// Try to find adder, but don't override preconfigured one
if ($addMethodNeeded) {
$addMethod = $this->checkMethod($reflClass, 'add' . $singular);
}

if ($adder->isPublic() && $adder->getNumberOfRequiredParameters() === 1
&& $remover->isPublic() && $remover->getNumberOfRequiredParameters() === 1) {
// Try to find remover, but don't override preconfigured one
if ($removeMethodNeeded) {
$removeMethod = $this->checkMethod($reflClass, 'remove' . $singular);
}

// We found a public, one-parameter add and remove method
// Found all that we need. Abort search.
if ((!$addMethodNeeded || $addMethod) && (!$removeMethodNeeded || $removeMethod)) {
break;
}

// False alert
$adder = null;
$remover = null;
$addMethod = null;
$removeMethod = null;
}
}

// Set preconfigured adder
if ($this->allowAdd && $this->addMethod) {
$addMethod = $this->checkMethod($reflClass, $this->addMethod);

if (!$addMethod) {
throw new FormException(sprintf(
'The method "%s" could not be found on class %s',
$this->addMethod,
$reflClass->getName()
));
}
}

// Set preconfigured remover
if ($this->allowDelete && $this->removeMethod) {
$removeMethod = $this->checkMethod($reflClass, $this->removeMethod);

if (!$removeMethod) {
throw new FormException(sprintf(
'The method "%s" could not be found on class %s',
$this->removeMethod,
$reflClass->getName()
));
}
}
}
Expand All @@ -136,17 +174,17 @@ public function onBindNormData(FilterDataEvent $event)
}
}

if ($adder && $remover) {
if ($addMethod || $removeMethod) {
// If methods to add and to remove exist, call them now, if allowed
if ($this->allowDelete) {
if ($removeMethod) {
foreach ($itemsToDelete as $item) {
$remover->invoke($parentData, $item);
$parentData->$removeMethod($item);
}
}

if ($this->allowAdd) {
if ($addMethod) {
foreach ($itemsToAdd as $item) {
$adder->invoke($parentData, $item);
$parentData->$addMethod($item);
}
}
} elseif (!$originalData) {
Expand Down Expand Up @@ -176,4 +214,16 @@ public function onBindNormData(FilterDataEvent $event)

$event->setData($originalData);
}

private function checkMethod(\ReflectionClass $reflClass, $methodName) {
if ($reflClass->hasMethod($methodName)) {
$method = $reflClass->getMethod($methodName);

if ($method->isPublic() && $method->getNumberOfRequiredParameters() === 1) {
return $methodName;
}
}

return null;
}
}
8 changes: 4 additions & 4 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public function buildForm(FormBuilder $builder, array $options)
// adders/removers
// Same as in CollectionType
$options['by_reference'],
$options['adder_prefix'],
$options['remover_prefix']
$options['add_method'],
$options['remove_method']
))
;
} else {
Expand Down Expand Up @@ -156,8 +156,8 @@ public function getDefaultOptions(array $options)
'empty_data' => $multiple || $expanded ? array() : '',
'empty_value' => $multiple || $expanded || !isset($options['empty_value']) ? null : '',
'error_bubbling' => false,
'adder_prefix' => 'add',
'remover_prefix' => 'remove',
'add_method' => null,
'remove_method' => null,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public function buildForm(FormBuilder $builder, array $options)
// is desired), disable support for adders/removers
// Same as in ChoiceType
$options['by_reference'],
$options['adder_prefix'],
$options['remover_prefix']
$options['add_method'],
$options['remove_method']
);

$builder
Expand Down Expand Up @@ -90,8 +90,8 @@ public function getDefaultOptions(array $options)
return array(
'allow_add' => false,
'allow_delete' => false,
'adder_prefix' => 'add',
'remover_prefix' => 'remove',
'add_method' => null,
'remove_method' => null,
'prototype' => true,
'prototype_name' => '__name__',
'type' => 'text',
Expand Down
Loading