Symfony表达式验证

时间:2018-06-10 10:57:56

标签: php symfony expression

如何向symfony表达式验证器添加自定义函数。例如,我写了这些表达式:"value in array_keys(this.getValues())"并想要添加array_keys函数。

我创建了自定义表达式语言类

<?php
declare(strict_types=1);

namespace App\ExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage as BaseExpressionLanguage;

class ValidatorExpressionLanguage extends BaseExpressionLanguage
{
    /**
     * {@inheritdoc}
     */
    protected function registerFunctions()
    {
        parent::registerFunctions();

        $this->addFunction(ExpressionFunction::fromPhp('array_keys'));
    }
}

但仍然有错误。(

1 个答案:

答案 0 :(得分:0)

解决:

validator.expression:
  class: Symfony\Component\Validator\Constraints\ExpressionValidator
  arguments:
    '$expressionLanguage': '@App\ExpressionLanguage\ValidatorExpressionLanguage'
    tags:
      - { name: 'validator.constraint_validator', alias: 'validator.expression' }