2013-05-07 5 views
3

양식 구성 요소없이 자체적으로 Symfony 검사기를 사용하고 있습니다.Symfony 하위 엔티티 만 유형으로 유효성 검사 중입니다. 자체 유효성 검사를 건너 뜁니다.

저는 하위 엔티티가 포함 된 엔티티가 있습니다. 현재이 필드가 하위 엔티티의 인스턴스임을 검증 할 수 있지만 제약 조건에 대해 하위의 유효성을 검사해야합니다.

#validation.yml 
# This is the entity I'm validating against, it checks the type but doesn't then validate 
# it against the child entity below. 
Greg\PropertyBundle\Entity\Property: 
    properties: 
     property_id: 
      - NotBlank: ~ 
      - Type: 
       type: string 
     addresses: 
      - All: 
       - Type: 
        type: Greg\PropertyBundle\Entity\Address 

# child entity 
Greg\PropertyBundle\Entity\Address: 
    properties: 
     city: 
      - NotBlank: ~ 
      - Type: 
       type: string 

내 서비스 중 하나에 DI와 함께 전달하고하고있어 발리를 호출하려면이 :

// Validate the data 
$errorList = $this->validator->validate($data); 

나는 또한 다음 플래그를 전달하여 그것을 시도했다 :

$errorList = $this->validator->validate($data, null, true, true); 
+1

는 속성 개체입니다, 당신은 $ 데이터의 유효성을 검사하는 것을 시도했다 - 주소? –

답변

6

기본적으로 유효성 검사는 속성의 개체에 대해 위임되지 않습니다. 유효성 검사 자식 객체에 대한 프로세스를 호출하려면 "Valid"라는 특정 제약 조건을 사용해야합니다.

그래서 유효성 검사 스크립트는 다음과 같습니다 "유효"제약 조건에 대한

#validation.yml 
# This is the entity I'm validating against, it checks the type but doesn't then validate 
# it against the child entity below. 
Greg\PropertyBundle\Entity\Property: 
    properties: 
     property_id: 
      - NotBlank: ~ 
      - Type: 
       type: string 
     addresses: 
      - All: 
       - Type: 
        type: Greg\PropertyBundle\Entity\Address 
      # addresses is array of entities, so use "traverse" option to validate each entity in that array 
      - Valid: { traverse: true } 

# child entity 
Greg\PropertyBundle\Entity\Address: 
    properties: 
     city: 
      - NotBlank: ~ 
      - Type: 
       type: string 

자세한 내용은 여기에서 찾을 수 있습니다 : $ 데이터를 가정 http://symfony.com/doc/current/reference/constraints/Valid.html

+0

정말 고마워. 하나의 주소 (객체 배열이 아니라 객체를 참조하는 속성)가있는 경우 다음을 수행 할 수도 있습니다. - 유효 : ~ (트래버스 옵션 없음). – Daniel