How can I clear form values after successful form submission?
These didn't help:
CONTROLLER:
namespace CarBrandBundleController;
use CarBrandBundleEntityBrandEntity;
use CarBrandBundleFormTypeBrandType;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
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;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…