加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 13:57:43 所属栏目:PHP教程 来源:网络整理
导读:如何使用Symfony验证验证数组键? 说我有以下,电子邮件数组的每个键都是一个ID.如何使用回调或其他一些约束来验证它们(例如,正则表达式约束而不是回调)? $input = [ 'emails' = [ 7 = 'david@panmedia.co.nz',12 = 'some@email.add',],'user' = 'bob','amou
如何使用Symfony验证验证数组键?

说我有以下,电子邮件数组的每个键都是一个ID.如何使用回调或其他一些约束来验证它们(例如,正则表达式约束而不是回调)?

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

use SymfonyComponentValidatorValidation;
use SymfonyComponentValidatorConstraints;
$validator = Validation::createValidator();
$constraint = new ConstraintsCollection(array(
    'emails' => new ConstraintsAll(array(
        new ConstraintsEmail(),)),'user' => new ConstraintsRegex('/[a-z]/i'),'amount' => new ConstraintsRange(['min' => 5,'max' => 10]),));

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

(使用最新的dev-master symfony)

我将创建一个自定义验证约束,该约束对数组中的每个键 – 值对(或键只需要)应用约束.与“所有”约束类似,但是对键值对执行验证,而不是仅对值进行验证.
namespace GLSDemoBundleValidatorConstraints;
use SymfonyComponentValidatorConstraint;
use SymfonyComponentValidatorExceptionConstraintDefinitionException;

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 GLSDemooBundleValidatorConstraints;
use SymfonyComponentValidatorConstraint;
use SymfonyComponentValidatorConstraintValidator;
use SymfonyComponentValidatorExceptionUnexpectedTypeException;


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.']');
            }
        }
    }
}

我猜,只有回调约束适用于每个键值对,在那里你放置验证逻辑.

use GLSDemoBundleValidatorConstraintsAssocAll;

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

                    //your validation logic goes here
                    //...
                }
            ))),'user' => new ConstraintsRegex('/^[a-z]+$/i'),$constraint);
var_dump($violations);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读