Symfony\Component\Form\FieldGroup::addError PHP Method

addError() public method

{@inheritDoc}
public addError ( Symfony\Component\Form\FieldError $error, Symfony\Component\Form\PropertyPathIterator $pathIterator = null, $type = null )
$error Symfony\Component\Form\FieldError
$pathIterator Symfony\Component\Form\PropertyPathIterator
    public function addError(FieldError $error, PropertyPathIterator $pathIterator = null, $type = null)
    {
        if ($pathIterator !== null) {
            if ($type === self::FIELD_ERROR && $pathIterator->hasNext()) {
                $pathIterator->next();

                if ($pathIterator->isProperty() && $pathIterator->current() === 'fields') {
                    $pathIterator->next();
                }

                if ($this->has($pathIterator->current()) && !$this->get($pathIterator->current())->isHidden()) {
                    $this->get($pathIterator->current())->addError($error, $pathIterator, $type);

                    return;
                }
            } else if ($type === self::DATA_ERROR) {
                $iterator = new RecursiveFieldsWithPropertyPathIterator($this);
                $iterator = new \RecursiveIteratorIterator($iterator);

                foreach ($iterator as $field) {
                    if (null !== ($fieldPath = $field->getPropertyPath())) {
                        if ($fieldPath->getElement(0) === $pathIterator->current() && !$field->isHidden()) {
                            if ($pathIterator->hasNext()) {
                                $pathIterator->next();
                            }

                            $field->addError($error, $pathIterator, $type);

                            return;
                        }
                    }
                }
            }
        }

        parent::addError($error);
    }

Usage Example

Beispiel #1
0
 public function testAddErrorMapsErrorsOntoFieldsInAnonymousGroups()
 {
     // 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('Message'), array(), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR));
     $group = new FieldGroup('author');
     $group2 = new FieldGroup('anonymous', array('property_path' => null));
     $group2->add($field);
     $group->add($group2);
     $path = new PropertyPath('address');
     $group->addError('Message', array(), $path->getIterator(), FieldGroup::DATA_ERROR);
 }