processSubmitErrors
Opened this issue · 1 comments
Hi guys,
I'm new to using liform-react but I think it's really fantastic! Unfortunately I can not quite understand how to use the processSubmitErrors function I saw in the example in: https://github.com/Limenius/symfony-react-sandbox. As in the example in case the form is not valid, I return the normalized form and pass it to the function but it does not seem to work. Can someone give me some advice?
Thank you.
$serializer = $this->get('serializer');$em = $this->getDoctrine()->getManager();
$label = new Label();
$form = $this->createForm(LabelType::class, $label);
$data = json_decode($request->getContent(), true);
$form->submit($data);
if($form->isValid() && $form->isSubmitted()) {
$em->persist($label);
$em->flush();
return new JsonResponse(array(
'success' => 'Success!!'
));
}
return new JsonResponse($serializer->normalize($form), 400);
return fetch(baseUrl + "api/label/new", {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(label)
}).then( (response) => {
return response.json();
}).then( (data) => {
processSubmitErrors(data);
dispatch(labelsActions.addLabel(label));
});
You also need to serialize the errors to be returned in the json response.
In my use case, i format the json like this (this is just a copy paste from my trait implementing the serializatin of form, errors and initial values)
`
protected function liformize(FormInterface $form)
{
return [
'schema' => $this->getLiformSchema($form),
'values' => $this->getLiformInitialValues($form),
'errors' => $this->getLiformErrors($form)
];
}
protected function getLiformSchema(FormInterface $form)
{
$liform = new Liform($this->getResolver());
return $liform->transform($form);
}
protected function getLiformInitialValues(FormInterface $form)
{
$serializer = $this->get('liform.serializer.initial_values_normalizer');
return $serializer->normalize($form);
}
protected function getLiformErrors(FormInterface $form)
{
$serializer = $this->get('liform.serializer.form_error_normalizer');
return $serializer->normalize($form);
}`
I do return new JsonResponse(['form' => $this->liformize($form)])
from my controller
and then i pass back the form.errors to processSubmitErrors and that's it