/** * @dataProvider provideVirtualFormErrorTests */ public function testVirtualFormErrorMapping($target, $childName, $childPath, $grandChildName, $grandChildPath, $violationPath) { $violation = $this->getConstraintViolation($violationPath); $parent = $this->getForm('parent'); $child = $this->getForm($childName, $childPath, null, array(), true); $grandChild = $this->getForm($grandChildName, $grandChildPath); $parent->add($child); $child->add($grandChild); $this->mapper->mapViolation($violation, $parent); if (self::LEVEL_0 === $target) { $this->assertEquals(array($this->getFormError()), $parent->getErrors(), $parent->getName() . ' should have an error, but has none'); $this->assertFalse($child->hasErrors(), $childName . ' should not have an error, but has one'); $this->assertFalse($grandChild->hasErrors(), $grandChildName . ' should not have an error, but has one'); } elseif (self::LEVEL_1 === $target) { $this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one'); $this->assertEquals(array($this->getFormError()), $child->getErrors(), $childName . ' should have an error, but has none'); $this->assertFalse($grandChild->hasErrors(), $grandChildName . ' should not have an error, but has one'); } else { $this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one'); $this->assertFalse($child->hasErrors(), $childName . ' should not have an error, but has one'); $this->assertEquals(array($this->getFormError()), $grandChild->getErrors(), $grandChildName. ' should have an error, but has none'); } }
public function testBacktrackIfSeveralSubFormsWithSamePropertyPath() { $parent = $this->getForm('parent'); $child1 = $this->getForm('subform1', 'address'); $child2 = $this->getForm('subform2', 'address'); $child3 = $this->getForm('subform3', null, null, array(), true); $child4 = $this->getForm('subform4', null, null, array(), true); $grandChild1 = $this->getForm('street'); $grandChild2 = $this->getForm('street', '[sub_address1_street]'); $grandChild3 = $this->getForm('street', '[sub_address2_street]'); $parent->add($child1); $parent->add($child2); $parent->add($child3); $parent->add($child4); $child2->add($grandChild1); $child3->add($grandChild2); $child4->add($grandChild3); $parent->submit(array()); $violation1 = $this->getConstraintViolation('data.address[street]'); $violation2 = $this->getConstraintViolation('data[sub_address1_street]'); $violation3 = $this->getConstraintViolation('data[sub_address2_street]'); $this->mapper->mapViolation($violation1, $parent); $this->mapper->mapViolation($violation2, $parent); $this->mapper->mapViolation($violation3, $parent); $this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one'); $this->assertCount(0, $child1->getErrors(), $child1->getName() . ' should not have an error, but has one'); $this->assertCount(0, $child2->getErrors(), $child2->getName() . ' should not have an error, but has one'); $this->assertCount(0, $child3->getErrors(), $child3->getName() . ' should not have an error, but has one'); $this->assertCount(0, $child4->getErrors(), $child4->getName() . ' should not have an error, but has one'); $this->assertEquals(array($this->getFormError($violation1, $grandChild1)), iterator_to_array($grandChild1->getErrors()), $grandChild1->getName() . ' should have an error, but has none'); $this->assertEquals(array($this->getFormError($violation2, $grandChild2)), iterator_to_array($grandChild2->getErrors()), $grandChild2->getName() . ' should have an error, but has none'); $this->assertEquals(array($this->getFormError($violation3, $grandChild3)), iterator_to_array($grandChild3->getErrors()), $grandChild3->getName() . ' should have an error, but has none'); }
/** * Shows the basket. * * @param Form $form * * @return \Symfony\Component\HttpFoundation\Response */ public function indexAction($form = null) { $form = $form ?: $this->createForm('sonata_basket_basket', $this->get('sonata.basket'), array('validation_groups' => array('elements'))); // always validate the basket if (!$form->isBound()) { if ($violations = $this->get('validator')->validate($form)) { $violationMapper = new ViolationMapper(); foreach ($violations as $violation) { $violationMapper->mapViolation($violation, $form, true); } } } $this->get('session')->set('sonata_basket_delivery_redirect', 'sonata_basket_delivery_address'); $this->get('sonata.seo.page')->setTitle($this->get('translator')->trans('basket_index_title', array(), 'SonataBasketBundle')); return $this->render('SonataBasketBundle:Basket:index.html.twig', array('basket' => $this->get('sonata.basket'), 'form' => $form->createView())); }
/** * Validate if the Alias is valid and unique. * * @param FormEvent $event */ public function validate(FormEvent $event) { /** @var Alias $alias */ $alias = $event->getData()->getNode()->alias; if ($alias instanceof ValueHolderInterface) { $alias = $alias->getWrappedValueHolderValue(); } $violations = $this->validator->validate($alias); if (!$violations) { return; } $mapper = new ViolationMapper(); /** @var ConstraintViolationInterface $violation */ foreach ($violations as $violation) { $mapper->mapViolation($violation, $event->getForm(), true); } }
public function testBacktrackIfSeveralSubFormsWithSamePropertyPath() { $violation = $this->getConstraintViolation('data.address[street]'); $parent = $this->getForm('parent'); $child1 = $this->getForm('subform1', 'address'); $child2 = $this->getForm('subform2', 'address'); $grandChild = $this->getForm('street'); $parent->add($child1); $parent->add($child2); $child2->add($grandChild); $this->mapper->mapViolation($violation, $parent); // The error occurred on the child of the second form with the same path $this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one'); $this->assertCount(0, $child1->getErrors(), $child1->getName() . ' should not have an error, but has one'); $this->assertCount(0, $child2->getErrors(), $child2->getName() . ' should not have an error, but has one'); $this->assertEquals(array($this->getFormError()), $grandChild->getErrors(), $grandChild->getName() . ' should have an error, but has none'); }
/** * @dataProvider provideErrorTestsForFormInheritingParentData */ public function testErrorMappingForFormInheritingParentData($target, $childName, $childPath, $grandChildName, $grandChildPath, $violationPath) { $violation = $this->getConstraintViolation($violationPath); $parent = $this->getForm('parent'); $child = $this->getForm($childName, $childPath, null, array(), true); $grandChild = $this->getForm($grandChildName, $grandChildPath); $parent->add($child); $child->add($grandChild); $parent->submit(array()); $this->mapper->mapViolation($violation, $parent); if (self::LEVEL_0 === $target) { $this->assertEquals(array($this->getFormError($violation, $parent)), iterator_to_array($parent->getErrors()), $parent->getName() . ' should have an error, but has none'); $this->assertCount(0, $child->getErrors(), $childName . ' should not have an error, but has one'); $this->assertCount(0, $grandChild->getErrors(), $grandChildName . ' should not have an error, but has one'); } elseif (self::LEVEL_1 === $target) { $this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one'); $this->assertEquals(array($this->getFormError($violation, $child)), iterator_to_array($child->getErrors()), $childName . ' should have an error, but has none'); $this->assertCount(0, $grandChild->getErrors(), $grandChildName . ' should not have an error, but has one'); } else { $this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one'); $this->assertCount(0, $child->getErrors(), $childName . ' should not have an error, but has one'); $this->assertEquals(array($this->getFormError($violation, $grandChild)), iterator_to_array($grandChild->getErrors()), $grandChildName . ' should have an error, but has none'); } }