5

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 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.