2016-07-20 134 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(); 
      } 


     } 

    }