8000 [Form] Fixed forms not to be marked invalid if their children are already marked invalid by webmozart · Pull Request #5947 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fixed forms not to be marked invalid if their children are already marked invalid #5947

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 1 commit into from
Nov 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,35 @@ public function validate($form, Constraint $constraint)
}
}
} else {
$clientDataAsString = is_scalar($form->getViewData())
? (string) $form->getViewData()
: gettype($form->getViewData());
$childrenSynchronized = true;

// Mark the form with an error if it is not synchronized
$this->context->addViolation(
$config->getOption('invalid_message'),
array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')),
$form->getViewData(),
null,
Form::ERR_INVALID
);
foreach ($form as $child) {
if (!$child->isSynchronized()) {
$childrenSynchronized = false;
break;
}
}

// Mark the form with an error if it is not synchronized BUT all
// of its children are synchronized. If any child is not
// synchronized, an error is displayed there already and showing
// a second error in its parent form is pointless, or worse, may
// lead to duplicate errors if error bubbling is enabled on the
// child.
// See also https://github.com/symfony/symfony/issues/4359
if ($childrenSynchronized) {
$clientDataAsString = is_scalar($form->getViewData())
? (string) $form->getViewData()
: gettype($form->getViewData());

$this->context->addViolation(
$config->getOption('invalid_message'),
array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')),
$form->getViewData(),
null,
Form::ERR_INVALID
);
}
}

// Mark the form with an error if it contains extra fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,37 @@ function () { throw new TransformationFailedException(); }
$this->validator->validate($form, new Form());
}

// https://github.com/symfony/symfony/issues/4359
public function testDontMarkInvalidIfAnyChildIsNotSynchronized()
{
$context = $this->getExecutionContext();
$object = $this->getMock('\stdClass');

$failingTransformer = new CallbackTransformer(
function ($data) { return $data; },
function () { throw new TransformationFailedException(); }
);

$form = $this->getBuilder('name', '\stdClass')
->setData($object)
->addViewTransformer($failingTransformer)
->setCompound(true)
->setDataMapper($this->getDataMapper())
->add(
$this->getBuilder('child')
->addViewTransformer($failingTransformer)
)
->getForm();

// Launch transformer
$form->bind(array('child' => 'foo'));

$this->validator->initialize($context);
$this->validator->validate($form, new Form());

$this->assertCount(0, $context->getViolations());
}

public function testHandleCallbackValidationGroups()
{
$context = $this->getExecutionContext();
Expand Down
0