简体   繁体   English

如何在Symfony2自定义验证器中获取请求值

[英]How to get the request value in the Symfony2 custom validator

I have a custom validator in Symfony2 and I would like to get one of the submited values from my form. 我在Symfony2中有一个自定义验证器,我想从表单中获取提交的值之一。

When vardumping the $value from 当从中推销$ value时

public function validate($value, Constraint $constraint)

I can see the variable I need, but the result is too big in order to be able to find where is my request parameter placed. 我可以看到所需的变量,但是结果太大,无法找到我的请求参数放在哪里。

Thank you in advance. 先感谢您。

I have found a workaround to this problem: 我找到了解决此问题的方法:

  1. I created a new field in my entity that does not exists in the database, but exists in the form. 我在实体中创建了一个新字段,该字段在数据库中不存在,但以表格形式存在。

  2. In the validator class, I am accessing all the entity values, and also this new one that I need. 在验证器类中,我正在访问所有实体值,以及我需要的这个新值。

FYI, in order to access all the entity object in the validator, you need to put: 仅供参考,为了访问验证器中的所有实体对象,您需要输入:

public function getTargets()
{
    return self::CLASS_CONSTRAINT;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM