Reputation: 442
I would like to already show errors of a form before the form is submitted. I have forms where the database data is incomplete so I would like to show what's already missing before they try to save it.
However $form->isValid() only executes when the form is also submitted (which only happens on post).
I tried the validator but that gives me a list of errors without adding them to the form and it seems a bad work around if I do that myself, however I was unable to figure out form the source how to achieve this.
$form = $this->get('form.factory')->create(SchoolFormType::class, $school, $formOptions);
$form->handleRequest($request);
if ($form->isValid()) {
// ...
}
Upvotes: 4
Views: 1129
Reputation: 3135
You can simulate a submit with :
$form->handleRequest($request);
$wasSubmitted = $form->isSubmitted();
if(!$wasSubmitted){
$form->submit($form->getData(),false);
}
if ($form->isValid()) {
// ...
// next lines are to be sure that the form will always need to be submitted manualy :
if($wasSubmitted){
$em->persist($entity);
$em->flush();
return $this->render ...
}
}
return $this->render ...
See the submit method in the source code.
Upvotes: 1