Symfony2-设置嵌入表单的最小数量

Symfony2-设置嵌入表单的最小数量,symfony,Symfony,使用中的场景,我希望确保任务始终至少有一个标记。然而在我的例子中,Task和Tag的关系是1:n而不是n:m 我特别关注的是所有标记都被删除的场景(我想防止这种情况发生)。如何确保任务表单始终至少有1个标记?我将创建一个自定义验证器,用于检查relationshop在实体级别映射到的属性在集合中是否有某些元素 验证程序失败: public function isValid($value, Constraint $constraint) { if (count($va

使用中的场景,我希望确保
任务
始终至少有一个
标记
。然而在我的例子中,
Task
Tag
的关系是
1:n
而不是
n:m


我特别关注的是所有
标记都被删除的场景(我想防止这种情况发生)。如何确保
任务
表单始终至少有1个
标记

我将创建一个自定义验证器,用于检查relationshop在实体级别映射到的属性在集合中是否有某些元素

验证程序失败:

   public function isValid($value, Constraint $constraint)
    {
        if (count($value) <1 ) {
            //also define a message for your custom validator
            $this->setMessage($constraint->message, array('%string%' => $value));
            return false;
        }
        return true;
    }
公共函数有效($value,Constraint$Constraint)
{
if(count($value)setMessage($constraint->message,数组('%string%'=>$value));
返回false;
}
返回true;
}

有关如何实现此自定义验证器的说明:

我将创建一个自定义验证器,用于检查relationshop在实体级别映射到的属性在集合中是否包含某些元素

验证程序失败:

   public function isValid($value, Constraint $constraint)
    {
        if (count($value) <1 ) {
            //also define a message for your custom validator
            $this->setMessage($constraint->message, array('%string%' => $value));
            return false;
        }
        return true;
    }
公共函数有效($value,Constraint$Constraint)
{
if(count($value)setMessage($constraint->message,数组('%string%'=>$value));
返回false;
}
返回true;
}

有关如何实现此自定义验证程序的说明:

您是否只想在发布时验证,是否至少有1个标记

或者您希望表单在加载时实际上已经有1个空标记吗? (我假设你说“如何确保一个任务表单始终至少有一个标记?”)

如果你需要第二个,只要

$tag1 = new Tag();
$tag1->name = 'tag1';
$task->getTags()->add($tag1);
以前

$form = $this->createForm(new TaskType(), $task);

就像文档中说的那样..

你想在发布时验证一下吗,是否至少有一个标签

或者您希望表单在加载时实际上已经有1个空标记吗? (我假设你说“如何确保一个任务表单始终至少有一个标记?”)

如果你需要第二个,只要

$tag1 = new Tag();
$tag1->name = 'tag1';
$task->getTags()->add($tag1);
以前

$form = $this->createForm(new TaskType(), $task);
正如文档所说,..

正如解决方案所指出的那样,确实是利用了自定义的验证约束。但是,我发现Symfony2.1中已经存在这样的约束验证器,所以我冒昧地将其移植到2.0(因为有些接口在2.1中显然发生了变化)

下面是用于计数集合的
Count.php
CountValidator.php
的移植版本(2.0版)(请参阅)

Count.php CountValidator.php
名称空间MyVendor\MyBundle\Validator\Constraints;
使用Symfony\Component\Validator\Constraint;
使用Symfony\Component\Validator\ConstraintValidator;
使用Symfony\Component\Validator\Exception\UnexpectedTypeException;
类CountValidator扩展了ConstraintValidator
{
/**
*{@inheritardoc}
*/
公共函数有效($value,Constraint$Constraint)
{
如果(null==$value){
返回false;
}
如果(!is_数组($value)&&!$value instanceof\Countable){
抛出新的UnexpectedTypeException($value,'array或\Countable');
}
$count=计数($value);
如果($constraint->min==$constraint->max
&&$count!=$constraint->min){
$this->setMessage($constraint->exactMessage,数组(
{{count}}=>$count,
{{limit}}=>$constraint->min,
));
返回false;
}
如果(null!=$constraint->max&&$count>$constraint->max){
$this->setMessage($constraint->maxMessage,数组)(
{{count}}=>$count,
{{limit}}=>$constraint->min,
));
返回false;
}
如果(null!=$constraint->min&&$count<$constraint->min){
$this->setMessage($constraint->minMessage,数组(
{{count}}=>$count,
{{limit}}=>$constraint->min,
));
返回false;
}
返回true;
}
}
正如解决方案所指出的那样,确实是利用了自定义的验证约束。但是,我发现Symfony2.1中已经存在这样的约束验证器,所以我冒昧地将其移植到2.0(因为有些接口在2.1中显然发生了变化)

下面是用于计数集合的
Count.php
CountValidator.php
的移植版本(2.0版)(请参阅)

Count.php CountValidator.php
名称空间MyVendor\MyBundle\Validator\Constraints;
使用Symfony\Component\Validator\Constraint;
使用Symfony\Component\Validator\ConstraintValidator;
使用Symfony\Component\Validator\Exception\UnexpectedTypeException;
类CountValidator扩展了ConstraintValidator
{
/**
*{@inheritardoc}
*/
公共函数有效($value,Constraint$Constraint)
{
如果(null==$value){
返回false;
}
如果(!is_数组($value)&&!$value instanceof\Countable){
抛出新的UnexpectedTypeException($value,'array或\Countable');
}
$count=计数($value);
如果($constraint->min==$constraint->max
&&$count!=$constraint->min){
$this->setMessage($constraint->exactMessage,数组(
{{count}}=>$count,
{{limit}}=>$constraint->min,
));
返回false;
}
如果(null!=$constraint->max&&$count>$constraint->max){
$this->setMessage($constraint->maxMessage,数组)(
{{count}}=>$count,
{{limit}}=>$constraint->min,
));
返回false;
}
如果(null!=$constraint->min&&$count<$constraint->min){
$this->setMessage($constraint->minMessage,数组(
{{count}}=>$count,
'{{