2013-09-23 84 views
0

我有一个使用嵌入表单集合的表单。表单嵌入验证

在我的主窗体中,我对字段“comment”进行了验证。这个验证很简单,工作正常。 我的嵌入式表单处理另一个实体。我想对这个实体领域

| comment (min length = 5 ok) ------ 
            | anotherfield (min length = 5 not ok)  
            | anotherfield (min length = 5 not ok) 

我调用由validation.yml文件两种形式验证规则验证:

My\Bundle\Entity\Main: 
    properties: 
     comment: 
      - Length: 
       min: 5 
       minMessage: "minmessage" 

My\Bundle\Entity\EmbedEntity: 
    properties: 
     anotherfield: 
      - Length: 
       min: 5 
       minMessage: "minmessage" 

但是第二次验证不理我的形式是COMMITED。 (没有错误返回,并通过$form is->valid()

我的验证文件被读取。 (我对评论的第一次验证是好的)

我错过了什么吗?

回答

0

"cascade_validation" => true在您的父母表单应该使嵌入式表单被验证。

此外,我认为你可以添加一个Valid到你的验证文件中的嵌入式字段以使其起作用。

+0

它似乎并没有做到这一点。 – goto

0

使用有效约束来验证作为父对象属性嵌入的对象

例如,如果使用注释

/** 
* 
* @Assert\Valid 
*/ 
private $items; 
0

添加 'error_bubbling'=>为真,该属性,也显示了minMessages。 例如:

$builder->add('title', null, array('error_bubbling'=>true,"mapped" => true, "description" => "The title of the position")) 

在总汇builder->添加通话:

$builder->add(
      'positionOwners', 
      'collection', 
      array(
       'type' => new PositionOwnerType($this->positionOwnerFormSubscriber), 
       'allow_add' => true, 
       'allow_delete' => true, 
       'mapped' => true, 
       'error_bubbling'=>true, 
      'cascade_validation' => true 
      ) 
     ) 

此外,setDefaultOptions应该是这样的:

/** 
* Set the default options of PositionType form 
* @param OptionsResolverInterface $resolver 
*/ 
public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(
     array(
      'data_class' => 'Radsphere\RecruitmentBundle\Entity\PositionType', 
      'csrf_protection' => false, 
      'cascade_validation' => true, 
      'error_bubbling'=>true 
     ) 
    ); 
} 
+0

已经尝试过,它没有工作:/ – goto