我想验证$ form-> isValid()之外的一些变量,但收到以下错误消息:
函数的参数太少 App \ Validator \ Constraints \ ContainsTestValidator :: __ construct(),0 传入**** \供应商\ symfony \ validator \ ConstraintValidatorFactory.php 在第43行,正好是1个
控制器:
class TestController extends AbstractController
{
public function validate(Request $request)
{
$string= $request->request->get('data');
$validator = Validation::createValidator();
$constraint = new Assert\ContainsTest();
$violations = $validator->validate($string, $constraint);
}
}
ContainsTest:
class ContainsTest extends Constraint
{
public $message = '"{{ string }}" is not valid.';
}
包含TestValidator:
class ContainsTestValidator extends ConstraintValidator
{
private $entityManager;
/**
* @param EntityManager $entityManager
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function validate($value, Constraint $constraint)
{
/* some code */
}
}
有人知道如何初始化EntityManagerInterface吗?
答案 0 :(得分:1)
解决了。我有同样的问题。
这是一个提示https://github.com/symfony/symfony/issues/27760
但是,在构造函数中TestController
注入Symfony\Component\Validator\Validator\ValidatorInterface
的真正需求是什么。
之后,使用此界面代替Validation::createValidator();
这里应该是这样:
class TestController extends AbstractController
{
private $validator;
public function __construct( ValidatorInterface $validator)
{
$this->validator = $validator;
}
public function validate(Request $request)
{
$string= $request->request->get('data');
$validator = $this->validator;
$constraint = new Assert\ContainsTest();
$violations = $validator->validate($string, $constraint);
}
}