2012-01-30 60 views

回答

0

为拥有该集合的实体编写自定义回调验证程序。

如果你有车实体和产品,例如集合,你应该做的:

............... 
use Symfony\Component\Validator\Constraints as Assert; 
............... 
* @Assert\Callback(
* methods={"hasCorrectNumberOfProducts"} 
*) 
class Cart 
{ 
........... 

public function hasCorrectNumberOfProducts(ExecutionContext $context) 
{ 
    $propertyPath = $context->getPropertyPath(); 
    $correct = 666; 

    if(!count($this->getProducts()) == $correct) { 
     $context->setPropertyPath($propertyPath . '.products'); 
     $context->addViolation('Incorrect number of products!', array(), null); 
    } 
} 
...... 
相关问题