2016-08-26 2 views
1

사용자 정의 유효성 검사기에서 유효성이 검사 된 엔티티 클래스를 가져올 수 있는지 알고 싶습니다. 아니 당신은 할 수 없습니다사용자 정의 유효성 검사기에서 엔티티 클래스 가져 오기

use Symfony\Component\Validator\Constraint; 

class UniqueKey extends Constraint { 
    public $message = 'The string "%string%" is not good'; 

    public function validatedBy() { 
    return get_class($this).'Validator'; 
    } 
} 

class UniqueKeyValidator extends ConstraintValidator { 
    public function validate($value, Constraint $constraint) { 
    // I would like to get the class of the entity validated. Can I? 
    } 
} 
+0

[* Class Constraint Validator *] (https://symfony.com/doc/current/validation/custom_constraint.html#class-constraint-validator)로 만들 수 있습니까? – Yoshi

답변

1

mlwacosmos

귀하의 유효성은 검증되고 루트 개체에 액세스 할 수 있습니다. 그런 다음, 다음 루트 개체에 액세스해야 할 것이다 : 이것은 양식을 통해, 루트 개체는 형태가 될 것이며, 당신의 엔티티 양식 데이터에 사용할 수 인 경우

class UniqueKeyValidator extends ConstraintValidator { 
    public function validate($value, Constraint $constraint) { 
     // This will return the root object 
     $object = $this->context->getRoot(); 
    } 
} 

$object->getData();

희망이 도움이됩니다.

+0

ok ... 시도해 볼 것입니다. 저장소에 액세스 할 수 있도록 교리 서비스를받을 수 있습니까? – mlwacosmos

+0

Validator를 서비스로 구성하는 방법이있는 것처럼 보입니다. http://symfony.com/doc/current/validation/custom_constraint.html#constraint-validators-with-dependencies – rouflak

+0

유효성 검사기가 서비스이기는하지만 내 엔터티에 annotaiton을 추가 할 때 서비스로 호출되지 않습니다. – mlwacosmos

0

: 여기

는 사용자 정의 유효성 검사기입니다. Constraint에서 엔터티를 조작하려면 Class 제약 조건을 만들어 엔터티에 바인딩해야합니다.

관련 문제