Assert validate empty array collection symfony

Viewed 3265

Is there a way to validate and check an array of collection if it is or not empty. I already tried :

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


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

}
1 Answers

Try with the Count assert

// 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 = [];
}

Validates that a given collection’s (i.e. an array or an object that implements Countable) element count is between some minimum and maximum value.

Do not specify max if you do not need it.

Related