class FormErrorIterator implements RecursiveIterator, SeekableIterator, ArrayAccess, Countable

Iterates over the errors of a form.

Optionally, this class supports recursive iteration. In order to iterate recursively, set the constructor argument $deep to true. Now each element returned by the iterator is either an instance of {@link FormError} or of {@link FormErrorIterator}, in case the errors belong to a sub-form.

You can also wrap the iterator into a {@link \RecursiveIteratorIterator} to flatten the recursive structure into a flat list of errors.

Constants

INDENTATION

The prefix used for indenting nested error messages.

Methods

__construct(FormInterface $form, array $errors)

Creates a new iterator.

string
__toString()

Returns all iterated error messages as string.

getForm()

Returns the iterated form.

current()

Returns the current element of the iterator.

next()

Advances the iterator to the next position.

int
key()

Returns the current position of the iterator.

bool
valid()

Returns whether the iterator's position is valid.

rewind()

Sets the iterator's position to the beginning.

bool
offsetExists(int $position)

Returns whether a position exists in the iterator.

offsetGet(int $position)

Returns the element at a position in the iterator.

offsetSet($position, $value)

Unsupported method.

offsetUnset($position)

Unsupported method.

bool
hasChildren()

Returns whether the current element of the iterator can be recursed into.

getChildren()

Alias of {@link current()}.

int
count()

Returns the number of elements in the iterator.

seek(int $position)

Sets the position of the iterator.

findByCodes(string|string[] $codes)

Creates iterator for errors with specific codes.

Details

at line line 50
__construct(FormInterface $form, array $errors)

Creates a new iterator.

Parameters

FormInterface $form The erroneous form
array $errors The form errors

Exceptions

InvalidArgumentException If the errors are invalid

at line line 72
string __toString()

Returns all iterated error messages as string.

Return Value

string The iterated error messages

at line line 94
FormInterface getForm()

Returns the iterated form.

Return Value

FormInterface The form whose errors are iterated by this object

at line line 105
FormError|FormErrorIterator current()

Returns the current element of the iterator.

Return Value

FormError|FormErrorIterator an error or an iterator containing nested errors

at line line 113
next()

Advances the iterator to the next position.

at line line 123
int key()

Returns the current position of the iterator.

Return Value

int The 0-indexed position

at line line 133
bool valid()

Returns whether the iterator's position is valid.

Return Value

bool Whether the iterator is valid

at line line 144
rewind()

Sets the iterator's position to the beginning.

This method detects if errors have been added to the form since the construction of the iterator.

at line line 156
bool offsetExists(int $position)

Returns whether a position exists in the iterator.

Parameters

int $position The position

Return Value

bool Whether that position exists

at line line 170
FormError|FormErrorIterator offsetGet(int $position)

Returns the element at a position in the iterator.

Parameters

int $position The position

Return Value

FormError|FormErrorIterator The element at the given position

Exceptions

OutOfBoundsException If the given position does not exist

at line line 184
offsetSet($position, $value)

Unsupported method.

Parameters

$position
$value

Exceptions

BadMethodCallException

at line line 194
offsetUnset($position)

Unsupported method.

Parameters

$position

Exceptions

BadMethodCallException

at line line 205
bool hasChildren()

Returns whether the current element of the iterator can be recursed into.

Return Value

bool Whether the current element is an instance of this class

at line line 213
getChildren()

Alias of {@link current()}.

at line line 235
int count()

Returns the number of elements in the iterator.

Note that this is not the total number of errors, if the constructor parameter $deep was set to true! In that case, you should wrap the iterator into a {@link \RecursiveIteratorIterator} with the standard mode {@link \RecursiveIteratorIterator::LEAVES_ONLY} and count the result.

$iterator = new \RecursiveIteratorIterator($form->getErrors(true));
$count = count(iterator_to_array($iterator));

Alternatively, set the constructor argument $flatten to true as well.

$count = count($form->getErrors(true, true));

Return Value

int The number of iterated elements

at line line 247
seek(int $position)

Sets the position of the iterator.

Parameters

int $position The new position

Exceptions

OutOfBoundsException If the position is invalid

at line line 267
FormErrorIterator findByCodes(string|string[] $codes)

Creates iterator for errors with specific codes.

Parameters

string|string[] $codes The codes to find

Return Value

FormErrorIterator new instance which contains only specific errors