Returns a new iterator for this path
public getIterator ( ) : Symfony\Component\Form\PropertyPathIterator | ||
return | Symfony\Component\Form\PropertyPathIterator |
public function createAction() { // bind form to page model $page = new Page(); $this->form->bind($this->request, $page); if ($this->form->isValid()) { try { // path for page $parent = $this->form->get('parent')->getData(); $path = $parent . '/' . $page->name; // save page $this->dm->persist($page, $path); $this->dm->flush(); // redirect with message $this->request->getSession()->setFlash('notice', 'Page created!'); return $this->redirect($this->generateUrl('admin')); } catch (HTTPErrorException $e) { $path = new PropertyPath('name'); $this->form->addError(new DataError('Name already in use.'), $path->getIterator()); } } return $this->render('SandboxAdminBundle:Admin:create.html.twig', array('form' => $this->form)); }
/** * Validates the form and its domain object * * @throws FormException If the option "validator" was not set */ public function validate() { $validator = $this->getOption('validator'); if (null === $validator) { throw new MissingOptionsException('The option "validator" is required for validating', array('validator')); } // Validate the form in group "Default" // Validation of the data in the custom group is done by validateData(), // which is constrained by the Execute constraint if ($violations = $validator->validate($this)) { foreach ($violations as $violation) { $propertyPath = new PropertyPath($violation->getPropertyPath()); $iterator = $propertyPath->getIterator(); $template = $violation->getMessageTemplate(); $parameters = $violation->getMessageParameters(); if ($iterator->current() == 'data') { $iterator->next(); // point at the first data element $error = new DataError($template, $parameters); } else { $error = new FieldError($template, $parameters); } $this->addError($error, $iterator); } } }
/** * Binds the form with values and files. * * This method is final because it is very easy to break a form when * overriding this method and adding logic that depends on $taintedFiles. * You should override doBind() instead where the uploaded files are * already merged into the data array. * * @param array $taintedValues The form data of the $_POST array * @param array $taintedFiles An array of uploaded files * @return boolean Whether the form is valid */ public final function bind($taintedValues, array $taintedFiles = null) { if (null === $taintedFiles) { if ($this->isMultipart() && $this->getParent() === null) { throw new \InvalidArgumentException('You must provide a files array for multipart forms'); } $taintedFiles = array(); } if (null === $taintedValues) { $taintedValues = array(); } $this->doBind(self::deepArrayUnion($taintedValues, $taintedFiles)); if ($this->getParent() === null) { if ($this->validator === null) { throw new FormException('A validator is required for binding. Forgot to pass it to the constructor of the form?'); } if ($violations = $this->validator->validate($this, $this->getValidationGroups())) { // TODO: test me foreach ($violations as $violation) { $propertyPath = new PropertyPath($violation->getPropertyPath()); $iterator = $propertyPath->getIterator(); if ($iterator->current() == 'data') { $type = self::DATA_ERROR; $iterator->next(); // point at the first data element } else { $type = self::FIELD_ERROR; } $this->addError(new FieldError($violation->getMessageTemplate(), $violation->getMessageParameters()), $iterator, $type); } } } }
public function testAddErrorMapsErrorsOntoFieldsInAnonymousGroups() { $error = new FieldError('Message'); // path is expected to point at "address" $expectedPath = new PropertyPath('address'); $expectedPathIterator = $expectedPath->getIterator(); $field = $this->createMockField('address'); $field->expects($this->any())->method('getPropertyPath')->will($this->returnValue(new PropertyPath('address'))); $field->expects($this->once())->method('addError')->with($this->equalTo($error), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR)); $group = new TestFieldGroup('author'); $group2 = new TestFieldGroup('anonymous', array('property_path' => null)); $group2->add($field); $group->add($group2); $path = new PropertyPath('address'); $group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR); }
public function testAddErrorMapsErrorsOntoFieldsInVirtualGroups() { $error = new DataError('Message'); // path is expected to point at "address" $expectedPath = new PropertyPath('address'); $expectedPathIterator = $expectedPath->getIterator(); $field = $this->createMockField('address'); $field->expects($this->any())->method('getPropertyPath')->will($this->returnValue(new PropertyPath('address'))); $field->expects($this->once())->method('addError')->with($this->equalTo($error), $this->equalTo($expectedPathIterator)); $form = new Form('author'); $nestedForm = new Form('nested', array('virtual' => true)); $nestedForm->add($field); $form->add($nestedForm); $path = new PropertyPath('address'); $form->addError($error, $path->getIterator()); }