Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Form] getErrorsAsArray for Form class#7977

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
TomiS wants to merge1 commit intosymfony:masterfromRohea:master

Conversation

@TomiS
Copy link

With this new handy method Form class is now able to provide all validation errors of a form (and its child forms) in a beautiful associative array tree that can, for example, easily be serialized to JSON for API responses and such. It seems quite a few people (including me) have had problems with the lack of this feature. At least this is the impression I had while browsing through questions in stackoverflow etc.

Implementation is similar to existing helper methodgetErrorsAsString so this one should behave however it behaves.

QA
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes (...I think. There were errors/fails but not caused by my code)
Fixed tickets
LicenseMIT
Doc PR

So... Any future for this?

@TomiS
Copy link
Author

Here's an example of output (serialized in JSON for better readability)

{    "title": [        "Field cannot be empty."    ],    "category": [        "Value must be 0 or more."    ],    "addresses": [        {            "street": [                "Field cannot be empty."            ]        }    ]}

@raziel057
Copy link
Contributor

I already created a PR for this feature but there is place for improvements. See#7512

@TomiS
Copy link
Author

@raziel057 Oh... I guess I should have searched existing PRs a bit more intensively before coding :) Anyway, just adding weight for importance of this then.

@TomiSTomiS closed thisMay 8, 2013
@TomiS
Copy link
Author

Btw. Here's a similar workaround implementation for those who need it right now:

    public function getErrorsAsArray($form)    {        $errors = array();        foreach ($form->getErrors() as $error) {            $errors[] = $error->getMessage();        }        foreach ($form->all() as $key => $child) {            if ($err = $this->getErrorsAsArray($child)) {                $errors[$key] = $err;            }        }        return $errors;    }

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@TomiS@raziel057

[8]ページ先頭

©2009-2025 Movatter.jp