Symfony 4.1 - 验证一个字段以检查电子邮件或电话号码

时间:2018-06-08 07:49:31

标签: symfony validation symfony4

我使用Symfony 4.1并且我想要一个字段,名称的标识符,它可以接收电子邮件或一个电话号码。

所以我想要有效,如果字段是电子邮件,或者字段是电话号码。

$constraint = new Assert\Collection([
    'op'    => new Assert\Required([
        new Assert\Choice(['choices' => Operators::toArray()]),
    ]),
    'value' => [
        new Assert\Required([
            new Assert\Collection([
                'identifier' => [
                    new Assert\Email(),
                ],
            ]),
        ]),
    ],
]);

return $this->validator->validate($data, $constraint);

我发送的身体是:

[
    {
        "op": "add",
        "value":  {
            "identifier": "john.doe@example.com",
            "identifierType": "email"
        }
    },
    {
        "op": "add",
        "value":  {
            "identifier": "+33600000000",
            "identifierType": "phone"
        }
    }
]

0 个答案:

没有答案