Skip to content

Commit

Permalink
[Form] added support for groups in form validation (when using array …
Browse files Browse the repository at this point in the history
…data)
  • Loading branch information
fabpot committed Apr 19, 2011
1 parent 5242859 commit 3ca5f51
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions src/Symfony/Component/Form/Validator/DelegatingValidator.php
Expand Up @@ -45,7 +45,11 @@ public function validate(FormInterface $form)
// Validation of the data in the custom group is done by validateData(),
// which is constrained by the Execute constraint
if ($form->hasAttribute('validation_constraint')) {
$violations = $this->validator->validateValue($form->getData(), $form->getAttribute('validation_constraint'));
$violations = $this->validator->validateValue(
$form->getData(),
$form->getAttribute('validation_constraint'),
self::getFormValidationGroups($form)
);
} else {
$violations = $this->validator->validate($form);
}
Expand Down Expand Up @@ -185,29 +189,6 @@ private function resolveMappingPlaceholders(array &$mapping, array $forms)
public static function validateFormData(FormInterface $form, ExecutionContext $context)
{
if (is_object($form->getData()) || is_array($form->getData())) {
$groups = null;

if ($form->hasAttribute('validation_groups')) {
$groups = $form->getAttribute('validation_groups');
}

$currentForm = $form;
while (!$groups && $currentForm->hasParent()) {
$currentForm = $currentForm->getParent();

if ($currentForm->hasAttribute('validation_groups')) {
$groups = $currentForm->getAttribute('validation_groups');
}
}

if (null === $groups) {
$groups = array('Default');
}

if (!is_array($groups)) {
$groups = array($groups);
}

$propertyPath = $context->getPropertyPath();
$graphWalker = $context->getGraphWalker();

Expand All @@ -222,9 +203,37 @@ public static function validateFormData(FormInterface $form, ExecutionContext $c

$propertyPath .= 'data';

foreach ($groups as $group) {
foreach (self::getFormValidationGroups($form) as $group) {
$graphWalker->walkReference($form->getData(), $group, $propertyPath, true);
}
}
}
}

static protected function getFormValidationGroups(FormInterface $form)
{
$groups = null;

if ($form->hasAttribute('validation_groups')) {
$groups = $form->getAttribute('validation_groups');
}

$currentForm = $form;
while (!$groups && $currentForm->hasParent()) {
$currentForm = $currentForm->getParent();

if ($currentForm->hasAttribute('validation_groups')) {
$groups = $currentForm->getAttribute('validation_groups');
}
}

if (null === $groups) {
$groups = array('Default');
}

if (!is_array($groups)) {
$groups = array($groups);
}

return $groups;
}
}

1 comment on commit 3ca5f51

@trompette
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why getFormValidationGroups() is static and in DelegatingValidator class ?
Maybe it should be located in the form framework, what do you think ?

Please sign in to comment.