gpt4 book ai didi

php - 如何使用 Symfony Validation 验证数组键?

转载 作者:IT王子 更新时间:2023-10-29 00:06:01 24 4
gpt4 key购买 nike

如何使用 Symfony 验证来验证数组键?

假设我有以下内容,emails 数组的每个键都是一个 ID。我如何使用回调或其他一些约束(例如正则表达式约束而不是回调)来验证它们?

$input = [
'emails' => [
7 => 'david@panmedia.co.nz',
12 => 'some@email.add',
],
'user' => 'bob',
'amount' => 7,
];

use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints;
$validator = Validation::createValidator();
$constraint = new Constraints\Collection(array(
'emails' => new Constraints\All(array(
new Constraints\Email(),
)),
'user' => new Constraints\Regex('/[a-z]/i'),
'amount' => new Constraints\Range(['min' => 5, 'max' => 10]),
));

$violations = $validator->validateValue($input, $constraint);
echo $violations;

(使用最新的 dev-master symfony)

最佳答案

我会创建一个自定义验证约束,它对数组中的每个键值对(或仅在需要时使用键)应用约束。类似于 All 约束,但验证是在键值对上执行的,而不仅仅是值。

namespace GLS\DemoBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

class AssocAll extends Constraint
{
public $constraints = array();

public function __construct($options = null)
{
parent::__construct($options);

if (! is_array($this->constraints)) {
$this->constraints = array($this->constraints);
}

foreach ($this->constraints as $constraint) {
if (!$constraint instanceof Constraint) {
throw new ConstraintDefinitionException('The value ' . $constraint . ' is not an instance of Constraint in constraint ' . __CLASS__);
}
}
}

public function getDefaultOption()
{
return 'constraints';
}

public function getRequiredOptions()
{
return array('constraints');
}
}

约束验证器,它将带有键值对的数组传递给每个约束:

namespace GLS\DemooBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;


class AssocAllValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (null === $value) {
return;
}

if (!is_array($value) && !$value instanceof \Traversable) {
throw new UnexpectedTypeException($value, 'array or Traversable');
}

$walker = $this->context->getGraphWalker();
$group = $this->context->getGroup();
$propertyPath = $this->context->getPropertyPath();

foreach ($value as $key => $element) {
foreach ($constraint->constraints as $constr) {
$walker->walkConstraint($constr, array($key, $element), $group, $propertyPath.'['.$key.']');
}
}
}
}

我想,只有 Callback 约束才有意义应用于每个键值对,您可以在其中放置验证逻辑。

use GLS\DemoBundle\Validator\Constraints\AssocAll;

$validator = Validation::createValidator();
$constraint = new Constraints\Collection(array(
'emails' => new AssocAll(array(
new Constraints\Callback(array(
'methods' => array(function($item, ExecutionContext $context) {
$key = $item[0];
$value = $item[1];

//your validation logic goes here
//...
}
))),
)),
'user' => new Constraints\Regex('/^[a-z]+$/i'),
'amount' => new Constraints\Range(['min' => 5, 'max' => 10]),
));

$violations = $validator->validateValue($input, $constraint);
var_dump($violations);

关于php - 如何使用 Symfony Validation 验证数组键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16050240/

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