Skip to content

[Form] FormValidator removed code related to removed cascade_validation option #18081

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

Closed
wants to merge 2 commits into from
Closed
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 @@ -43,9 +43,10 @@ public function validate($form, Constraint $constraint)
if ($form->isSynchronized()) {
// Validate the form data only if transformation succeeded
$groups = self::getValidationGroups($form);
$data = $form->getData();

// Validate the data against its own constraints
if (self::allowDataWalking($form)) {
if ($form->isRoot() && (is_object($data) || is_array($data))) {
foreach ($groups as $group) {
$validator->atPath('data')->validate($form->getData(), null, $group);
}
Expand Down Expand Up @@ -114,38 +115,6 @@ public function validate($form, Constraint $constraint)
}
}

/**
* Returns whether the data of a form may be walked.
*
* @param FormInterface $form The form to test.
*
* @return bool Whether the graph walker may walk the data.
*/
private static function allowDataWalking(FormInterface $form)
{
$data = $form->getData();

// Scalar values cannot have mapped constraints
if (!is_object($data) && !is_array($data)) {
return false;
}

// Root forms are always validated
if ($form->isRoot()) {
return true;
}

// Non-root forms are validated if validation cascading
// is enabled in all ancestor forms
while (null !== ($form = $form->getParent())) {
if (!$form->getConfig()->getOption('cascade_validation')) {
return false;
}
}

return true;
}

/**
* Returns the validation groups of the given form.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,7 @@ public function testValidateConstraints()
$this->assertNoViolation();
}

public function testValidateIfParentWithCascadeValidation()
{
$object = $this->getMock('\stdClass');

$parent = $this->getBuilder('parent', null, array('cascade_validation' => true))
->setCompound(true)
->setDataMapper($this->getDataMapper())
->getForm();
$options = array('validation_groups' => array('group1', 'group2'));
$form = $this->getBuilder('name', '\stdClass', $options)->getForm();
$parent->add($form);

$form->setData($object);

$this->expectValidateAt(0, 'data', $object, 'group1');
$this->expectValidateAt(1, 'data', $object, 'group2');

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

$this->assertNoViolation();
}

public function testValidateIfChildWithValidConstraint()
public function testValidateChildIfValidConstraint()
{
$object = $this->getMock('\stdClass');

Expand All @@ -149,11 +127,11 @@ public function testValidateIfChildWithValidConstraint()
$this->assertNoViolation();
}

public function testDontValidateIfParentWithoutCascadeValidation()
public function testDontValidateIfParentWithoutValidConstraint()
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need these tests? Don't they duplicate other tests here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure, I thought it might be intended as it looks like the test for an explizit cascade without the option/constraint.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd appreciate if you could check the other test cases whether any of them duplicates this one. If that's the case, we can safely remove this. This was mainly designed in order to test the special treatment of the cascade_validation option.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From my understanding it looks like it is not duplicated as we have the related tests

  • testValidateConstraintsEvenIfNoValidConstraint
  • testValidateIfChildWithValidConstraint
  • testDontValidateIfParentWithoutValidConstraint

Copy link
Contributor

Choose a reason for hiding this comment

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

Let me phrase this another way: Is there any way to make these two tests fail (individually) without making at least one other test in this test class fail at the same time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems so, but I am actually a bit confused about the difference of testValidateConstraintsEvenIfNoValidConstraint and testDontValidateIfParentWithoutValidConstraint as it looks like in the first tests constraints in the child are validated even when there is no Valid constraint.

Copy link
Contributor

Choose a reason for hiding this comment

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

@peterrehm I guess the naming of the tests is confusing. We have two different cases:

  • the constraints option: Constraints set in this option are always validated (given their groups match).
  • the Valid constraint: This constraint (either set in the validation metadata - e.g. as annotation - or in the constraints option) results in the constraints defined ine the metadata of the data being validated.

Example:

// data_class = Customer

$form->add('address', FormType::class);
$form->get('address')->add('street', TextType::class, [
    'constraints' => new NotBlank(),
]);

class Customer
{
    private $address;
}

class Address
{
    /**
     * @Length(min=3)
     */
    private $street;
}

In this example, by default only the NotBlank constraint is validated. The constraints defined in the Address class, however, are not validated:

  • There is no Valid constraint in the constraints option of the address field in the form.
  • There is no Valid constraint on the Customer::$address property.

If you add the Valid constraint in either of these places, the Length constraint is validated as well.

We could clarify the tests for example by renaming testValidateConstraintsEvenIfNoValidConstraint to testValidateConstraintsOptionEvenIfParentWithoutValidConstraint etc.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks better! Could you name the three tests for the Valid constraint consistently?

  • testValidateChildIfValidConstraint
  • testDontValidateChildIfNoValidConstraint
  • testValidateConstraintsOptionEvenIfNoValidConstraint

{
$object = $this->getMock('\stdClass');

$parent = $this->getBuilder('parent', null, array('cascade_validation' => false))
$parent = $this->getBuilder('parent', null)
->setCompound(true)
->setDataMapper($this->getDataMapper())
->getForm();
Expand Down Expand Up @@ -183,13 +161,13 @@ public function testMissingConstraintIndex()
$this->assertNoViolation();
}

public function testValidateConstraintsEvenIfNoCascadeValidation()
public function testValidateConstraintsOptionEvenIfNoValidConstraint()
{
$object = $this->getMock('\stdClass');
$constraint1 = new NotNull(array('groups' => array('group1', 'group2')));
$constraint2 = new NotBlank(array('groups' => 'group2'));

$parent = $this->getBuilder('parent', null, array('cascade_validation' => false))
$parent = $this->getBuilder('parent', null)
->setCompound(true)
->setDataMapper($this->getDataMapper())
->getForm();
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy