Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

processSubmitErrors #43

Open
Ogek opened this issue Apr 20, 2018 · 1 comment
Open

processSubmitErrors #43

Ogek opened this issue Apr 20, 2018 · 1 comment

Comments

@Ogek
Copy link

Ogek commented Apr 20, 2018

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));
    });
@jav974
Copy link

jav974 commented Jun 11, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants