Skip to content

Commit da187e5

Browse files
committed
prevent infinite loop on errors not related to the form fields
1 parent 288356a commit da187e5

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/Console/Helper/FormHelper.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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\FormErrorIterator;
1011
use Symfony\Component\Form\FormInterface;
1112

1213
class FormHelper extends Helper
@@ -69,7 +70,11 @@ public function interactUsingForm(
6970
$data = $form->getData();
7071

7172
if (!$form->isValid()) {
72-
$output->write(sprintf('Invalid data provided: %s', $form->getErrors(true, false)));
73+
$formErrors = $form->getErrors(true, false);
74+
$output->write(sprintf('Invalid data provided: %s', $formErrors));
75+
if ($this->noErrorsCanBeFixed($formErrors)) {
76+
throw new \RuntimeException('Errors out of the form\'s scope - do you have validation constraints on properties not exposed to the form?');
77+
}
7378
array_map(
7479
function (FormInterface $formField) use (&$validFormFields) {
7580
if ($formField->isValid()) {
@@ -83,4 +88,17 @@ function (FormInterface $formField) use (&$validFormFields) {
8388

8489
return $data;
8590
}
91+
92+
/**
93+
* @param FormErrorIterator $errors
94+
* @return bool
95+
*/
96+
protected function noErrorsCanBeFixed(FormErrorIterator $errors)
97+
{
98+
// none of the remaining errors is related to a value of a form field
99+
return $errors->count() > 0 &&
100+
0 === count(array_filter(iterator_to_array($errors), function ($error) {
101+
return $error instanceof FormErrorIterator;
102+
}));
103+
}
86104
}

0 commit comments

Comments
 (0)