0

我对 Symfony2 框架相当陌生,并且无法在 twig 中显示错误。约束是使用设置的$form->createFormBuilder()->add()

当我运行时{{ form_errors(form) }}没有任何显示并且我不确定我做错了什么。

public function massEmailAction(Request $request)
{
    $data['errors'] = '';
    $form = array('message'=>'test', 'single_email_val'=>'', 'send_to'=>'');
    $form = $this->createFormBuilder($form)
    ->add('message', 'textarea', array('attr'=>array('style'=>"width: 800px; height: 500px;")))
    ->add('single_email_val', 'text', array('label'=>'Single Email', 'required'=>false, 
                                                            'constraints'=>array(
                                                               new NotBlank(),
                                                               new Length(array('min' => 3)),


                                                    ),
                                            'invalid_message'=>'email not valid',
                                            'error_bubbling'=>true,
                                            ))
    ->add('send_to', 'choice', array('choices'=>array(''=>'Select',
                                                      'single_email'=>'Send to Single Email',
                                                      'mass_email'=>'Send to All'
                                                    ),
                                                    'required'=>false,

    ))
    ->getForm();

    $data['form'] = $form->createView();

    if($request->isMethod('POST'))
    {
        $emailConstraint = new NotBlank();
        $formData = $form->bind($this->getRequest());
        // Get form data
        $post = $formData->getData();
        echo $post['single_email_val'];

        if(!$form->isValid())
        {

            echo 'not valid';
            //print_r($this->getErrorMessages($form));
            print_r($this->getErrorMessages($form));
            $data['errors'] = $this->getErrorMessages($form);
            // print_r($form->getErrors());
            //echo $form->getErrorsAsString();
        }
        else
        {

        }

    }

    return $this->render('TmarketMarketingBundle:Default:massEmail.html.twig', $data);
}
4

1 回答 1

1

按此顺序尝试

**$data['form'] = $form->createView();**
return $this->render('TmarketMarketingBundle:Default:massEmail.html.twig', $data);

您在绑定和验证数据之前获得表单视图

于 2013-04-13T15:32:43.657 回答