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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何使用Symfony验证验证数组键?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
如何使用Symfony验证验证数组键?

我有以下,邮件数组的每个键都是一个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)

我将创建一个自定义验证约束,该约束对数组中的每个键 – 值对(或键只需要)应用约束.与“所有”约束类似,但是对键值对执行验证,而不是仅对值进行验证.
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.']');
            }
        }
    }
}

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

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'),$constraint);
var_dump($violations);

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何使用Symfony验证验证数组键?全部内容,希望文章能够帮你解决php – 如何使用Symfony验证验证数组键?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。