8000 [Form] Add getErrorsAsArray method by raziel057 · Pull Request #7512 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Add getErrorsAsArray method #7512

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
27 changes: 27 additions & 0 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,33 @@ public function getErrorsAsString($level = 0)
return $errors;
}

/**
* Returns an array representation of all form errors (including children errors).
*
* @return array An array representation of all errors
*/
public function getErrorsAsArray()
{
$errors = array();
foreach ($this->errors as $key => $error) {
$template = $error->getMessageTemplate();
if (null !== $template) {
$parameters = $error->getMessageParameters();
$errors[$key] = strtr($template, $parameters);
Copy link
Contributor

Choose a reason for hiding this comment

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

How it pissible translate this messages?

Copy link
Member

Choose a reason for hiding this comment

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

It is not possible. :(
may be, we can introduce a little optional coupling by allow the end user to give a translatorInterface to getErrorsAsArray

so from the controller $form->getErrorsAsArray($translator);

Copy link
Contributor

Choose a reason for hiding this comment

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

@raziel057 Does one $key can contain more than 1 error? If yes - you should append they but not override

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Koc As you can see it on the tests I added, a field can contain multiple errors, but in this case there is only one error per $key. Do you have use cases with multiples error per $key?

@lyrixx Yes, I also thought about this solution. I think it's not so bad. I can add this option if @fabpot is ok for that.

} else {
$errors[$key] = $error->getMessage();
}
}

foreach ($this->children as $child) {
if (!$child->isValid()) {
$errors[$child->getName()] = $child->getErrorsAsArray();
}
}

return $errors;
}

/**
* {@inheritdoc}
*/
Expand Down
66 changes: 66 additions & 0 deletions src/Symfony/Component/Form/Tests/CompoundFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,72 @@ public function testGetErrorsAsStringDeep()
$this->assertEquals("name:\n ERROR: Error!\nfoo:\n No errors\n", $parent->getErrorsAsString());
}

public function testGetErrorsAsArrayDeep()
{
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
$this->markTestSkipped('The "HttpFoundation" component is not available');
}

$request = new Request(array(), array(), array(), array(), array(), array(
'REQUEST_METHOD' => 'GET',
));

$form = $this->getBuilder('')
->setCompound(true)
->setDataMapper($this->getDataMapper())
->addEventSubscriber(new BindRequestListener())
->getForm();

$firstName = $this->getBuilder('firstName')->getForm();

$form->add($firstName);
$form->add($this->getBuilder('lastName')->getForm());

$form->bind($request);

$firstName->addError(new FormError('Error 1'));
$firstName->addError(new FormError('Error 2'));

$expected = array(
'firstName' => array('Error 1', 'Error 2'),
);

$this->assertEquals($expected, $form->getErrorsAsArray());
}

public function testGetErrorsAsArrayWithTemplate()
{
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
$this->markTestSkipped('The "HttpFoundation" component is not available');
}

$request = new Request(array(), array(), array(), array(), array(), array(
'REQUEST_METHOD' => 'GET',
));

$form = $this->getBuilder('')
->setCompound(true)
->setDataMapper($this->getDataMapper())
->addEventSubscriber(new BindRequestListener())
->getForm();

$firstName = $this->getBuilder('firstName')->getForm();

$form->add($firstName);
$form->add($this->getBuilder('lastName')->getForm());

$form->bind($request);

$firstName->addError(new FormError('Error 1', 'Error {{ replacementTag }}', array('{{ replacementTag }}' => 'foo')));
$firstName->addError(new FormError('Error 2'));

$expected = array(
'firstName' => array('Error foo', 'Error 2'),
);

$this->assertEquals($expected, $form->getErrorsAsArray());
}

protected function createForm()
{
return $this->getBuilder()
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Form/Tests/SimpleFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,13 @@ public function testGetErrorsAsString()
$this->assertEquals("ERROR: Error!\n", $this->form->getErrorsAsString());
}

public function testGetErrorsAsArray()
{
$this->form->addError(new FormError('Error!'));

$this->assertEquals(array("Error!"), $this->form->getErrorsAsArray());
}

public function testFormCanHaveEmptyName()
{
$form = $this->getBuilder('')->getForm();
Expand Down
0