Skip to content

[LiveComponent] Improve form validation error messages in exceptions#3432

Open
PierreCapel wants to merge 1 commit intosymfony:2.xfrom
PierreCapel:live-component-error-messages
Open

[LiveComponent] Improve form validation error messages in exceptions#3432
PierreCapel wants to merge 1 commit intosymfony:2.xfrom
PierreCapel:live-component-error-messages

Conversation

@PierreCapel
Copy link
Copy Markdown

@PierreCapel PierreCapel commented Apr 3, 2026

Q A
Bug fix? no
New feature? yes
Deprecations? no
Documentation? no
License MIT

When calling $this->submitForm() manually in a LiveAction, if validation fails, an UnprocessableEntityHttpException is thrown. Currently, this exception contains a generic message: "Form validation failed in component."

This makes it difficult to debug failures in automated tests or during development, especially with large forms or collections, as we need to manually inspect the form state to find the cause.

This PR adds the the full field path and the specific violation message in the exception.

Example output:

Form validation failed:
blog_post_form.title: This value should not be blank.
blog_post_form.content: This value is too short. It should have 100 characters or more.
blog_post_form.comments.0.text: This value should not be blank.

@carsonbot carsonbot added Feature New Feature LiveComponent Status: Needs Review Needs to be reviewed labels Apr 3, 2026
@PierreCapel PierreCapel force-pushed the live-component-error-messages branch 3 times, most recently from cd6821b to a5a47a6 Compare April 3, 2026 21:20
@PierreCapel PierreCapel force-pushed the live-component-error-messages branch from a5a47a6 to 6158323 Compare April 3, 2026 21:22
Comment on lines +327 to +330
$errors = [];

foreach ($form->getErrors() as $error) {
$label = '' !== $prefix ? $prefix : 'form';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
$errors = [];
foreach ($form->getErrors() as $error) {
$label = '' !== $prefix ? $prefix : 'form';
$errors = [];
$label = '' !== $prefix ? $prefix : 'form';
foreach ($form->getErrors() as $error) {

It looks safe to put this outside the loop

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

Labels

Feature New Feature LiveComponent Status: Needs Review Needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants