Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DX] Add a JsonSerializable implementation to FormErrorIterator and FormError. #11309

Closed
@goabonga

Description

@goabonga

Every time when i need to return form errors to json I need to transform manually errors in my controller like:

/** @var array $errors */
$errors = array();
/** @var FormError $error */
foreach($form->getErrors(true) as $error)
{
    $errors[] = array('message'=>$error->getMessage());
}
$response = new JsonResponse();
$response->setData(array(
    'errors' => $errors
));

but with this simple implementation we can do:

/** @var JsonResponse $response */
$response = new JsonResponse();
$response->setData(array(
    'form'=>array(
            'errors' => $form->getErrors(true)
    )
));

I do not see if this implementation can have any adverse side effect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DXDX = Developer eXperience (anything that improves the experience of using Symfony)Form

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions