How to clear field value with Symfony2 forms

后端 未结 3 2132
日久生厌
日久生厌 2021-02-14 00:56

I\'m writing my own CAPTCHA class and when the form doesn\'t validate, I don\'t want to pre-populate the captcha input with the previous answer, for obvious reasons. I just want

3条回答
  •  深忆病人
    2021-02-14 01:30

    You can pass an incomplete entity to the action called when your control finds form invalid.

    public function updateAction(Request $request, $id)
    {
        $entity = $this->EM()->getRepository('Bundle:Entity')->find($id);
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Entity entity.');
        }
    
        $form = $this->createForm(new RecommendationType()
            ,$entity
            ,array(
                'attr'            => array(
                    ,'entity'         => $entity
                    )
                )
            );
    
        $form->bind($request);
        if ($form->isValid()) {
            $this->EM()->persist($entity);
            $this->EM()->flush();
    
            return $this->redirect($this->generateUrl('entity_show'
                ,array('id' => $id)));
        } else {
            $entity->setCapthca(Null);
        } 
    
        return $this->render('Bundle:Entity:edit.html.twig'
            ,array(
                'entity'            => $entity
                ,'form'             => $form->createView()
                )
            );
    }
    

    The create action would have similar modification.

提交回复
热议问题