0

I'm trying to show errors validation message with the old The information already entered , but the problém when the form is not valid and it's submitted else if ($form->isSubmitted()&& !$form->isValid()) : the old input content(old The information already entered) will disappear .
By the way i want after avery submition that the url end with #contact that's why i worked with this->redirect .

    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {

        $task = $form->getData();


        $em = $this->getDoctrine()->getManager();
        $em->persist($task);
        $em->flush();
        $this->get('session')->getFlashBag()->add(
            'notice',
            'Votre message est bien envoyé !'
        );
    } else if ($form->isSubmitted() && !$form->isValid()) {
        $errors = array();

        foreach ($form->all() as $key => $child) {
            if (!$child->isValid()) {
                foreach ($child->getErrors() as $error) {
                    $errors[$key] = $error->getMessage();
                }
            }
        }


        foreach ($errors as $key => $value) {
            $this->get('session')->getFlashBag()->add('error', $value);
        }


        return $this->redirect($this->generateUrl('index') . '?a#contact');
    }

    return $this->render('front/index.html.twig', array('form' => $form ->createView())); 
}
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
famas23
  • 2,072
  • 4
  • 17
  • 53

1 Answers1

1

You should only redirect to your index if the form is valid. Right now that redirect is occurring in } else if ($form->isSubmitted() && !$form->isValid()) {, which means a redirect will occur with invalid data.

Try:

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {

    $task = $form->getData();


    $em = $this->getDoctrine()->getManager();
    $em->persist($task);
    $em->flush();
    $this->get('session')->getFlashBag()->add(
        'notice',
        'Votre message est bien envoyé !'
    );
    return $this->redirect($this->generateUrl('index') . '?a#contact');

} else if ($form->isSubmitted() && !$form->isValid()) {
    $errors = array();

    foreach ($form->all() as $key => $child) {
        if (!$child->isValid()) {
            foreach ($child->getErrors() as $error) {
                $errors[$key] = $error->getMessage();
            }
        }
    }


    foreach ($errors as $key => $value) {
        $this->get('session')->getFlashBag()->add('error', $value);
    }
}


return $this->render('front/index.html.twig', array('form' => $form ->createView())); 

}

This way, if your form is not valid, it will return the render form again (with your errors included). You can see an example of a controller that follows that flow here.

Darkstarone
  • 4,590
  • 8
  • 37
  • 74
  • this works correct but i want that when the page load it goes automaticly to the end of the page that's why i add #contact to the url – famas23 May 29 '17 at 21:09
  • What if you tried adding `array('form' => $form ->createView())` as a parameter to the redirect? It should pass all the errors for the form to use regardless of where you're redirecting to. – Darkstarone May 29 '17 at 21:11
  • i already tried `this->redirect($this->generateUrl('index').'?a#contact' ,array('form' => $form ->createView()));` but it throws an error code , because the syntaxe is not correct , i think that wican't pass parameters into `redirect()` – famas23 May 29 '17 at 21:17
  • That syntax is incorrect, see [here](https://stackoverflow.com/questions/1496975/symfony-redirect-with-2-parameters), it should be: `this->redirect($this->generateUrl('index', ,array('form' => $form ->createView())).'?a#contact')` or something similar. However, I don't know how that will work with your particular hardcoded addition. – Darkstarone May 29 '17 at 21:18
  • didnt work ,because you passed paramters to generateUrl() not to redirect() , i told you we cant pass data to redirect live we pass them with render(), so we cant pass any parameter to the view – famas23 May 29 '17 at 21:22
  • If you can't pass parameters to the view, I'm not sure how you can pass errors (which are themselves parameters) I'm afraid. Hopefully, someone else can figure this out for you! – Darkstarone May 29 '17 at 21:24