2015-01-02 1 views
3

다른 필드 (3)가 옵션 'a'이고 3이 다른 필드 (2) 인 경우 필드 유효성 검사를 작성하려고합니다. '비'. 그 일을 어떻게 하죠?심포니는 다른 필드가 특정 값일 때 유효성을 검사합니다.

편집 : 엔티티 용입니다. 나는 노력하고있는 샘플을 올릴 것이다.

/** 
*@Assert\Collection(
*fields = { aName = @Assert\NotBlank(), 
*   aAmount = @Assert\NotBlank() } 
*/ 
protected $1; 

/** 
*@Assert\Collection(
*fields = { bName = @Assert\NotBlank(), 
*   bAmount = @Assert\NotBlank() } 
*/ 
protected $2; 

/** 
*@Assert\NotBlank() 
*/ 
protected $3; 

$ 3 == 'a'이면 $ 1, $ 3 == 'b'이면 $ 2가 필요합니다.

+0

유효성 검사의 어떤 종류를 사용합니까를? 어쩌면 당신은 당신의 코드를 보여줄 수 있습니다 – Ziumin

+0

이것은 많은 요인에 달려 있으며 더 많은 정보가 필요합니다. 예를 들어, 이것은 순수하게 양식입니까? 엔티티 용입니까? 동일한 엔티티의 필드입니까? 더 많은 정보를 제공 할 수 있습니까? –

답변

5

당신은 검증 제약 사용할 수 있습니다 Expression

예 :

/** 
* @Assert\Expression(
*  "not (this.getThird() == 'a' and this.getFirst() == null)", 
*  message="If third = 'a', first should be not null" 
*) 
*/ 
protected $first; 

/** 
* @Assert\Expression(
*  "not (this.getThird() == 'b' and this.getSecond() == null)", 
*  message="If third = 'b', second should be not null" 
*) 
*/ 
protected $second; 

protected $third; 

public function getFirst() 
{ 
    return $this->first; 
} 

public function getSecond() 
{ 
    return $this->second; 
} 

public function getThird() 
{ 
    return $this->third; 
} 
관련 문제