【问题标题】:Symfony not displaying errors in twigSymfony 未在树枝中显示错误
【发布时间】:2012-12-25 08:59:15
【问题描述】:

我对 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);
}

【问题讨论】:

  • 请给我们看 $form->getErrorsAsString();输出

标签: forms error-handling twig symfony-2.1


【解决方案1】:

按这个顺序试试

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多