2013-07-29 80 views
0

我想用symfony2构建一篇文章配置器。 在我的第一个实体中存储了所有可能的物品配置。基于另一个实体的数据验证学说实体

实体PossibleArticleConfiguration
名称:第一条
MINLENGTH个:250 的MaxLength:500
MinWidth:250
MaxWidth:500

在我的第二个实体的对象看起来是这样的:

实体

ConfiguredArticle

名称:第一条

长度:300

宽度:400

是否有验证基础上,最小和最大范围ConfiguredArticle对象的最佳做法在PossibleArticleConfiguration中?

在此先感谢

+0

是有像ConfiguredArticle的关系 - > PossibleArticleConfiguration(一到许多/一对音)? – nifr

+0

是的,有一个关系配置文章/材料OneToMany <-> ManyToOne PossibleArticleConfiguration/Material – user1731323

回答

0

的ConfiguredArticle实体里面可以有这样的方法:

public function isLengthValid(ExecutionContextInterface $context) { 
    if ($this->getLength < $this->getArticleConfiguration()->getMinLength()) { 
     $context->addViolationAt('length', 'The length does not satisfy the minimum length', array(), null); 
    } 
    if ($this->getLength > $this->getArticleConfiguration()->getMaxLength()) { 
     $context->addViolationAt('length', 'The length does not satisfy the maximum length', array(), null); 
    } 
} 

public function isWidthValid(ExecutionContextInterface $context) { 
    if ($this->getWidth < $this->getArticleConfiguration()->getMinWidth()) { 
     $context->addViolationAt('width', 'The width does not satisfy the minimum width', array(), null); 
    } 
    if ($this->getWidth > $this->getArticleConfiguration()->getMaxWidth()) { 
     $context->addViolationAt('width', 'The width does not satisfy the maximum width', array(), null); 
    } 
} 

了解更多关于如何在此页上使用回调方法进行验证:http://symfony.com/doc/current/reference/constraints/Callback.html