2013-09-23 86 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