断言验证空数组集合 symfony

fam*_*s23 3 assert symfony doctrine-orm

有没有办法验证和检查集合数组是否为空。我已经尝试过:

/**
 * @Assert\NotBlank()
 * @Assert\Length( min = 1)
 */
protected $workPlaces;


public function __construct()
{
    $this->workPlaces = new ArrayCollection();

}
Run Code Online (Sandbox Code Playgroud)

Tom*_*oms 6

尝试使用Count 断言

// src/Entity/Participant.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Participant
{
    /**
     * @Assert\Count(
     *      min = 1,
     *      max = 5,
     *      minMessage = "You must specify at least one email",
     *      maxMessage = "You cannot specify more than {{ limit }} emails"
     * )
     */
    protected $emails = [];
}
Run Code Online (Sandbox Code Playgroud)

验证给定集合(即实现 Countable 的数组或对象)元素计数是否在某个最小值和最大值之间。

max如果不需要,请不要指定。