Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I clear form values after successful form submission

Tags:

symfony

How can I clear form values after successful form submission?

These didn't help:

  • How to clear field value with Symfony2 forms
  • Clear form values after successful submit

CONTROLLER:

namespace Car\BrandBundle\Controller;

use Car\BrandBundle\Entity\BrandEntity;
use Car\BrandBundle\Form\Type\BrandType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class BrandController extends Controller
{
    public function indexAction()
    {
        $form = $this->getFrom();

        return $this->render('CarBrandBundle:Default:brand.html.twig',
                array('page' => 'Brand', 'form' => $form->createView(), 'brands' => $this->getBrands()));
    }

    public function createAction(Request $request)
    {
        if ($request->getMethod() != 'POST')
        {
            return new Response('Only POST method is allowed');
        }

        $form = $this->getFrom();

        $form->handleRequest($request);

        if ($form->isValid())
        {
            $submission = $form->getData();

            $em = $this->getDoctrine()->getManager();

            $brand = new BrandEntity();
            $brand->setName($submission->getName());

            $em->persist($brand);
            $em->flush();

            $this->redirect($this->generateUrl('brand'));
        }

        return $this->render('CarBrandBundle:Default:brand.html.twig',
                array('page' => 'Brand', 'form' => $form->createView(), 'brands' => $this->getBrands()));
    }

    private function getFrom()
    {
        return $this->createForm(new BrandType(), new BrandEntity(),
                array('action' => $this->generateUrl('brandCreate')));
    }

    private function getBrands()
    {
        $repo = $this->getDoctrine()->getRepository('CarBrandBundle:BrandEntity');
        $brands = $repo->findAll();

        return $brands;
    }
} 
like image 546
BentCoder Avatar asked Jun 15 '14 13:06

BentCoder


People also ask

How do I reset form values after submitting?

The reset() method resets the values of all elements in a form (same as clicking the Reset button). Tip: Use the submit() method to submit the form.

How do you clear all fields in form?

To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.

How do you clear fields after form submit in asp net c#?

You may use JavaScript form reset() method or loop throw all textboxes and set Text property to empty string.


2 Answers

You simply need to unset both form and entity objects. Then, create new, clean instances so they are available in your template. Personally I prefer to do it only when the form is properly validated.

if($form->isValid()){

  // persisting and flushing the entity

  unset($entity);
  unset($form);
  $entity = new Entity();
  $form = $this->createForm(new EntityType(), $entity);
}

Works for me. Cheers.

like image 110
BazylPL Avatar answered Oct 01 '22 14:10

BazylPL


Resetting symfony form when same page handles the submission

In Bazyl's method creating another form after unsetting current form may be unnecessary task. I suggest to redirect to same page because symfony documentation (handling-form-submissions) also shows an example which redirects to another controller.

    if ($form->isSubmitted() && $form->isValid()) {
        // ... perform some action, such as saving the task to the database
        return $this->redirect($request->getUri());
    }

I have added sample sources to GitHub

like image 24
TRiNE Avatar answered Oct 01 '22 13:10

TRiNE