2016-07-20 136 views
0

我有一個自定義約束,並用它來驗證實體的ArrayCollection:定製Contrain嫩枝

在用戶方i有:

* @ORM\OneToMany(targetEntity="UserFiles", mappedBy="user", cascade={"all"}) 
* @Assert\All(constraints={ @CustomAssert\FileEmpty }) 

在我的樹枝模板在那裏我寫{{ form_errors(form) }}我看到的錯誤因爲我想,但我不知道如何訪問它並單獨寫入。

在控制器我這樣做:

VarDumper::dump($form->getErrors()); 

,其結果是:

FormErrorIterator {#576 ▼ 
    -form: Form {#579 ▶} 
    -errors: array:1 [▶] 
} 

我如何可以訪問到我的錯誤在樹枝?

回答

0

爲了解決這個問題,我使用了不同的方法。 我將約束移至userFiles實體屬性。

我的問題是,我不知道如何訪問到提交的數據在類驗證,使我的闡述。 解決方法是:

public function validate($value, Constraint $constraint) 
    { 
     $userfiles = $this->context->getObject(); 


     if(!is_null($value)) 
     { 
      if(is_null($userfiles->getFile())) 
      { 
       $this->context->buildViolation($constraint->message)->addViolation(); 
      } 


     } 

    }