Skip to content

Commit b0ba80a

Browse files
committed
Ask new input when input is invalid
1 parent ca6ffce commit b0ba80a

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

src/Console/Helper/FormHelper.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,38 @@ public function __construct(
4444
*/
4545
public function interactUsingForm($formType, InputInterface $input, OutputInterface $output, array $options = [])
4646
{
47-
$form = $this->formFactory->create($formType, $input, $options);
47+
$data = null;
48+
$validFormFields = [];
4849

49-
$submittedData = $this->formInteractor->interactWith($form, $this->getHelperSet(), $input, $output);
50+
do {
51+
$form = $this->formFactory->create($formType, $input, $options);
52+
$form->setData($data);
5053

51-
$form->submit($submittedData);
52-
if (!$form->isValid()) {
53-
$this->invalidForm($form);
54-
}
54+
// if we are rerunning the form for invalid data we don't need the fields that are already valid.
55+
foreach ($validFormFields as $validFormField) {
56+
$form->remove($validFormField);
57+
}
5558

56-
return $form->getData();
57-
}
59+
$submittedData = $this->formInteractor->interactWith($form, $this->getHelperSet(), $input, $output);
5860

59-
/**
60-
* @param FormInterface $form
61-
*
62-
* @throws \RuntimeException
63-
*/
64-
private function invalidForm(FormInterface $form)
65-
{
66-
throw new \RuntimeException(sprintf('Invalid data provided: %s', $form->getErrors(true, false)));
61+
$form->submit($submittedData);
62+
63+
// save the current data
64+
$data = $form->getData();
65+
66+
if (!$form->isValid()) {
67+
$output->write(sprintf('Invalid data provided: %s', $form->getErrors(true, false)));
68+
array_map(
69+
function (FormInterface $formField) use ($form, &$validFormFields) {
70+
if ($formField->isValid()) {
71+
$validFormFields[] = $formField->getName();
72+
}
73+
},
74+
$form->all()
75+
);
76+
}
77+
} while (!$form->isValid());
78+
79+
return $data;
6780
}
6881
}

0 commit comments

Comments
 (0)