gpt4 book ai didi

symfony - 断言验证空数组集合 symfony

转载 作者:行者123 更新时间:2023-12-04 00:55:45 25 4
gpt4 key购买 nike

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

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


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

}

最佳答案

尝试使用 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 thatimplements Countable) element count is between some minimum andmaximum value.


不指定 max如果你不需要它。

关于symfony - 断言验证空数组集合 symfony,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62640301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com