Skip to content
Open
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
22 changes: 21 additions & 1 deletion src/LiveComponent/src/ComponentWithFormTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private function submitForm(bool $validateAll = true): void
);

if (!$form->isValid()) {
throw new UnprocessableEntityHttpException('Form validation failed in component.');
throw new UnprocessableEntityHttpException(\sprintf("Form validation failed:\n%s", implode("\n", $this->extractErrors($form, $form->getName()))));
}
}

Expand Down Expand Up @@ -318,4 +318,24 @@ private function clearErrorsForNonValidatedFields(FormInterface $form, string $c
$this->clearErrorsForNonValidatedFields($child, \sprintf('%s.%s', $currentPath, $name));
}
}

/**
* @return array<string>
*/
private function extractErrors(FormInterface $form, string $prefix = ''): array
{
$errors = [];
$label = '' !== $prefix ? $prefix : 'form';

foreach ($form->getErrors() as $error) {
$errors[] = $label.': '.$error->getMessage();
}

foreach ($form->all() as $name => $child) {
$childPath = '' !== $prefix ? \sprintf('%s.%s', $prefix, $name) : (string) $name;
array_push($errors, ...$this->extractErrors($child, $childPath));
}

return $errors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ public function removeComment(#[LiveArg] int $index)
{
unset($this->formValues['comments'][$index]);
}

#[LiveAction]
public function save(): void
{
$this->submitForm();
}
}
8 changes: 7 additions & 1 deletion src/LiveComponent/tests/Fixtures/Form/CommentFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\Comment;

use Symfony\Component\Validator\Constraints\NotBlank;

class CommentFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('content', TextareaType::class)
->add('content', TextareaType::class, [
'constraints' => [
new NotBlank(message: 'The comment content field should not be blank'),
],
])
;
}

Expand Down
37 changes: 37 additions & 0 deletions src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\UX\LiveComponent\Tests\Fixtures\Component\FormWithCollectionTypeComponent;
use Symfony\UX\LiveComponent\Tests\Fixtures\Entity\User;
use Symfony\UX\LiveComponent\Tests\Fixtures\Factory\CategoryFixtureEntityFactory;
Expand Down Expand Up @@ -488,4 +489,40 @@ public function testFormWithLivePropContainingAnEntityImplementingAnInterface()
self::assertEquals(1, $user->id);
self::assertEquals('Nicolas', $user->username);
}

public function testSubmitFormExceptionMessageContainsFieldPathsAndMessages()
{
$mounted = $this->mountComponent('form_with_collection_type');
$dehydratedProps = $this->dehydrateComponent($mounted)->getProps();

$exception = null;

try {
$this->browser()
->throwExceptions()
->post('/_components/form_with_collection_type/save', [
'body' => [
'data' => json_encode([
'props' => $dehydratedProps,
'updated' => [
'blog_post_form.title' => '',
'blog_post_form.content' => 'too short',
'blog_post_form.comments' => [['content' => '']],
],
]),
],
])
;
} catch (UnprocessableEntityHttpException $e) {
$exception = $e;
}

$this->assertNotNull($exception);
$this->assertStringContainsString('title', $exception->getMessage());
$this->assertStringContainsString('content', $exception->getMessage());
$this->assertStringContainsString('The title field should not be blank', $exception->getMessage());
$this->assertStringContainsString('The content field is too short', $exception->getMessage());
$this->assertStringContainsString('blog_post_form.comments.0.content', $exception->getMessage());
$this->assertStringContainsString('The comment content field should not be blank', $exception->getMessage());
}
}
Loading