77use Symfony \Component \Console \Helper \Helper ;
88use Symfony \Component \Console \Input \InputInterface ;
99use Symfony \Component \Console \Output \OutputInterface ;
10+ use Symfony \Component \Form \FormErrorIterator ;
1011use Symfony \Component \Form \FormInterface ;
1112
1213class 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