Symfony 4 as a backend, Angular as frontend. I have a simple form - let's say two fields which cannot be blank.
Entity:
/**
* @var string
*
* @ORM\Column(name="signature", type="string", length=32, unique=true)
* @JMSSerializer\Expose
*/
private $signature;
/**
* @var int
*
* @ORM\Column(name="status", type="integer")
* @JMSSerializer\Expose
*/
private $status;
So default is NOT NULL.
After leaving one of those fields blank and submitting the form I get 500 error. Dev.log says: "Integrity constraint violation: 1048 Column 'status' cannot be null". Obviously. But how to catch this error? Or maybe just prevent submitting a form with blank fields on Angular side?
My controller:
public function postAction(Request $request)
{
$form = $this->createForm('App\Form\ItemType', null, [
'csrf_protection' => false,
]);
$form->submit($request->request->all());
if (!$form->isValid()) {
return $form;
}
...
As suggested elsewhere I could send an array of errors:
$errors = [];
foreach ($form->getErrors(true) as $error) {
if ($error->getOrigin()) {
$errors[$error->getOrigin()->getName()][] = $error->getMessage();
}
}
But isValid() method is never called.
BTW) Is it a good idea to return a form as a response in this case? I have seen this in some tutorial. But maybe returning
new View(null, Response::HTTP_BAD_REQUEST)
with some additional info would be a better option?