2016-01-13 11 views
0

Symfony驗證程序似乎不起作用。我注意到服務validator.emailConstraintValidaotr::$context不返回ExecutionContextInterface實例,所以我得到的錯誤來自ConstraintValidator的上下文不會返回ExecutionContextInterface的實例

試圖調用名爲類的「buildViolation」「的Symfony \分量\驗證\約束\爲EmailValidator」一個未定義的方法。

通過自定義的約束驗證了同樣的情況:

<?php 
namespace MyAppBundle\Validator\Constraints; 

use Symfony\Component\Validator\Constraint; 

class Test extends Constraint 
{ 
    public $message = 'Error message'; 

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

    public function validatedBy() 
    { 
     return 'test_validator'; 
    } 
} 

和驗證

<?php 
namespace MyAppBundle\Validator\Constraints; 

use Symfony\Component\Validator\Constraint; 
use Symfony\Component\Validator\ConstraintValidator; 

class TestValidator extends ConstraintValidator 
{ 
    public function validate($value, Constraint $constraint) 
    { 
     $this->context->buildViolation('...')->addViolation('test'); 
    } 
} 

services.yml

validator.test: 
    class: MyAppBundle\Validator\Constraints\TestValidator 
    tags: 
     - { name: validator.constraint_validator, alias: test_validator } 

我得到了同樣的錯誤。 這

public function validate($value, Constraint $constraint) 
{ 
    echo get_class($this->context); 
    $this->context->buildViolation('...')->addViolation('test'); 
} 

的控制器:

$this->get('validator.test')->validate('', new Test()); 

說這方面是MyAppBundle\Validator\Constraints\TestValidator一個實例。沒有道理。

我正在使用Symfony 3.0.0。

+0

這看起來很奇怪。你在哪裏使用'validator.email'服務,爲什麼? – xabbuh

回答

0

我不知道爲什麼,但按照文檔教程,它不起作用。 這工作:

$errors = $this->get('validator')->validate($value, new Test()); 
相關問題