2015-01-04 27 views
1

我在Symfony2中有一個表單類,並且將驗證條件添加到表單的每個元素(而不是連接的實體對象)。我想爲某些元素添加驗證組,但我似乎無法弄清楚如何。如何在Symfony2中的特定表單元素上分配驗證組

我指定的形式確認團組這樣的:

public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'Acme\CommonBundle\Entity\Campaign', 
     'validation_groups' => array('Default', 'new_title') 
    )); 
} 

和特定的表單元素上我這樣做:

->add('title', 'text', array(
    'required' => true, 
    'help_text' => 'This is the title.', 
    'constraints' => array(new NotBlank(), new Length(array('min' => 3, 'max' => 150))), 
    'validation_groups' => array('new_title') 

然而,這似乎並不奏效。難道我做錯了什麼?

回答

1

可以分配一個驗證組到一個特定形式的元件(as described in the docs for current symfony2 version)所示:

->add('title', 'text', array(
'required' => true, 
'help_text' => 'This is the title.', 
'constraints' => array(new NotBlank(array('groups' => array('new_title')), new Length(array('min' => 3, 'max' => 150, 'groups' => array('new_title')))), 
相關問題