|
7 | 7 | use Symfony\Component\Console\Helper\Helper; |
8 | 8 | use Symfony\Component\Console\Input\InputInterface; |
9 | 9 | use Symfony\Component\Console\Output\OutputInterface; |
| 10 | +use Symfony\Component\Form\FormError; |
10 | 11 | use Symfony\Component\Form\FormErrorIterator; |
11 | 12 | use Symfony\Component\Form\FormInterface; |
| 13 | +use Symfony\Component\Validator\ConstraintViolationInterface; |
12 | 14 |
|
13 | 15 | class FormHelper extends Helper |
14 | 16 | { |
@@ -73,7 +75,12 @@ public function interactUsingForm( |
73 | 75 | $formErrors = $form->getErrors(true, false); |
74 | 76 | $output->write(sprintf('Invalid data provided: %s', $formErrors)); |
75 | 77 | if ($this->noErrorsCanBeFixed($formErrors)) { |
76 | | - throw new \RuntimeException('Errors out of the form\'s scope - do you have validation constraints on properties not used in the form?'); |
| 78 | + $violationPaths = $this->constraintViolationPaths($formErrors); |
| 79 | + $hint = (count($violationPaths) > 0 ? ' (Violations on unused fields: '.implode(', ', $violationPaths).')' : ''); |
| 80 | + throw new \RuntimeException( |
| 81 | + 'Errors out of the form\'s scope - do you have validation constraints on properties not used in the form?' |
| 82 | + . $hint |
| 83 | + ); |
77 | 84 | } |
78 | 85 | array_map( |
79 | 86 | function (FormInterface $formField) use (&$validFormFields) { |
@@ -101,4 +108,20 @@ protected function noErrorsCanBeFixed(FormErrorIterator $errors) |
101 | 108 | return $error instanceof FormErrorIterator; |
102 | 109 | })); |
103 | 110 | } |
| 111 | + |
| 112 | + protected function constraintViolationPaths(FormErrorIterator $errors) |
| 113 | + { |
| 114 | + $paths = []; |
| 115 | + foreach ($errors as $error) { |
| 116 | + if (!$error instanceof FormError) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + $cause = $error->getCause(); |
| 120 | + if (!$cause instanceof ConstraintViolationInterface) { |
| 121 | + continue; |
| 122 | + } |
| 123 | + $paths[] = $cause->getPropertyPath(); |
| 124 | + } |
| 125 | + return $paths; |
| 126 | + } |
104 | 127 | } |
0 commit comments