Skip to content

Commit 188ce3c

Browse files
committed
infinite loop prevention: property path hints
1 parent d9b4e13 commit 188ce3c

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

features/interactive.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,5 +221,5 @@ Feature: It is possible to interactively fill in a form from the CLI
221221
And the output should contain
222222
"""
223223
[RuntimeException]
224-
Errors out of the form's scope - do you have validation constraints on properties not used in the form?
224+
Errors out of the form's scope - do you have validation constraints on properties not used in the form? (Violations on unused fields: data.fieldNotUsedInTheForm)
225225
"""

src/Console/Helper/FormHelper.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use Symfony\Component\Console\Helper\Helper;
88
use Symfony\Component\Console\Input\InputInterface;
99
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Component\Form\FormError;
1011
use Symfony\Component\Form\FormErrorIterator;
1112
use Symfony\Component\Form\FormInterface;
13+
use Symfony\Component\Validator\ConstraintViolationInterface;
1214

1315
class FormHelper extends Helper
1416
{
@@ -73,7 +75,12 @@ public function interactUsingForm(
7375
$formErrors = $form->getErrors(true, false);
7476
$output->write(sprintf('Invalid data provided: %s', $formErrors));
7577
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+
);
7784
}
7885
array_map(
7986
function (FormInterface $formField) use (&$validFormFields) {
@@ -101,4 +108,20 @@ protected function noErrorsCanBeFixed(FormErrorIterator $errors)
101108
return $error instanceof FormErrorIterator;
102109
}));
103110
}
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+
}
104127
}

0 commit comments

Comments
 (0)