Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在方法参数中使用Symfony验证器_Php_Symfony_Symfony Validator - Fatal编程技术网

Php 在方法参数中使用Symfony验证器

Php 在方法参数中使用Symfony验证器,php,symfony,symfony-validator,Php,Symfony,Symfony Validator,有没有办法将Symfony验证器约束应用于方法的参数 大概是这样的: use Symfony\Component\Validator\Constraints as Assert; class acmeClass { /** * @param $argument * @Assert\Choice({"YES", "NO"}) */ public funciton foo($argument) { /.. } } 因此,如果$argument的值与给定的

有没有办法将Symfony验证器约束应用于方法的参数

大概是这样的:

use Symfony\Component\Validator\Constraints as Assert;

class acmeClass
{
  /**
  * @param $argument
  * @Assert\Choice({"YES", "NO"})
  */
  public funciton foo($argument) 
  {
    /..  
  }
}

因此,如果$argument的值与给定的选项不同,验证将引发异常?

不幸的是,Symfony框架不支持这一点

您有两种选择:

1) 。在调用
foo()
之前进行验证:

2) 。验证内部
foo()
本身:

class acmeClass
{
    public function foo($argument)
    {
        $errors = $validator->validate($argument, $constraint);
        if (count($errors) > 0) {
            //throw some exception?
        }

        //do something here with $argument
    }
}


老实说,我认为Symfony不支持这个更好。程序员在阅读代码时通常会忽略注释和注释,这些注释和注释可能会过时,从而导致代码调试困难。

不幸的是,Symfony框架不支持这一点

您有两种选择:

1) 。在调用
foo()
之前进行验证:

2) 。验证内部
foo()
本身:

class acmeClass
{
    public function foo($argument)
    {
        $errors = $validator->validate($argument, $constraint);
        if (count($errors) > 0) {
            //throw some exception?
        }

        //do something here with $argument
    }
}

老实说,我认为Symfony不支持这个更好。注释和注释通常在读取代码时经常被程序员忽略,并且它们常常会被遗漏,从而导致调试代码的难度