2012-09-14 25 views
70

我在我的控制器中检查了一些验证。而且我想在失败时将错误添加到我的表单的特定元素。我的表单:向Symfony 2表单元素添加错误

use Symfony\Component\Form\FormError; 

// ... 

$config = new Config(); 
$form = $this->createFormBuilder($config) 
     ->add('googleMapKey', 'text', array('label' => 'Google Map key')) 
     ->add('locationRadius', 'text', array('label' => 'Location radius (km)')) 
     ->getForm(); 

// ... 

$form->addError(new FormError('error message')); 

addError()方法添加错误形成,而不是元素。我如何向locationRadius元素添加错误?

回答

145

你可以做

$form->get('locationRadius')->addError(new FormError('error message')); 

表单元素也FormInterface类型。

+1

感谢。它解决了我的问题。 – pltvs

+0

@ m2mdas,很棒的回答!我们将如何翻译?因为一旦我们创建了一个FormError实例,它就不会翻译它,对吗?我试过了,它没有翻译它,我认为它是有道理的。你将如何翻译FormError实例? – Mick

+1

你好@Patt,对于迟到的回复感到抱歉。在将错误消息添加到表单之前,Validator组件负责转换表单约束违规消息。对于添加自定义错误,您可以像处理其他字符串一样翻译消息,例如'$ this-> get('translator') - > trans('error message')' –

5

好吧,我有另外一种方法。它比较复杂,只适用于特定情况。

我的情况:

我有一个表格,并提交后,我发布的数据API服务器。还有我从API服务器获得的错误。

API服务器错误格式为:

array(
    'message' => 'Invalid postal code', 
    'propertyPath' => 'businessAdress.postalCode', 
) 

我的目标是获得灵活的解决方案。让我们为相应的字段设置错误。

$vm = new ViolationMapper(); 

// Format should be: children[businessAddress].children[postalCode] 
$error['propertyPath'] = 'children['. str_replace('.', '].children[', $error['propertyPath']) .']'; 

// Convert error to violation. 
$constraint = new ConstraintViolation(
    $error['message'], $error['message'], array(), '', $error['propertyPath'], null 
); 

$vm->mapViolation($constraint, $form); 

就是这样!

注意!addError()方法绕过error_mapping选项。


我的形式(嵌入在公司形式地址形式):

公司

<?php 

namespace Acme\DemoBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\Validator\Constraints; 

class Company extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('companyName', 'text', 
       array(
        'label' => 'Company name', 
        'constraints' => array(
         new Constraints\NotBlank() 
        ), 
       ) 
      ) 
      ->add('businessAddress', new Address(), 
       array(
        'label' => 'Business address', 
       ) 
      ) 
      ->add('update', 'submit', array(
        'label' => 'Update', 
       ) 
      ) 
     ; 
    } 

    public function getName() 
    { 
     return null; 
    } 
} 

地址

<?php 

namespace Acme\DemoBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\Validator\Constraints; 

class Address extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      // ... 
      ->add('postalCode', 'text', 
       array(
        'label' => 'Postal code', 
        'constraints' => array(
         new Constraints\NotBlank() 
        ), 
       ) 
      ) 
      ->add('town', 'text', 
       array(
        'label' => 'Town', 
        'constraints' => array(
         new Constraints\NotBlank() 
        ), 
       ) 
      ) 
      ->add('country', 'choice', 
       array(
        'label' => 'Country', 
        'choices' => $this->getCountries(), 
        'empty_value' => 'Select...', 
        'constraints' => array(
         new Constraints\NotBlank() 
        ), 
       ) 
      ) 
     ; 
    } 

    public function getName() 
    { 
     return null; 
    } 
} 
+0

你在哪里放置这些代码?$ vm = new ViolationMapper(); –

+0

@vidyvideni ,控制器将处理表单提交的操作,并且您可以调整这段代码并将其移至单独的方法 – Jekis