2012-11-20 2 views
1

게시물 매개 변수 bill_ceck에 따라 청구서 주소의 유효성을 어떻게 확인할 수 있습니까?symfony 1.4 배송 주소의 조건부 유효 확인

게시물 유효성 검사 (http://symfony.com/legacy/doc/cookbook/1_2/en/conditional-validator)를 검토했지만 AND 검증과 OR 검증이 아닌 것 같습니다.

class OrderAddForm extends BaseOprOrderHeaderForm { 
    public function configure() { 
    $this->setWidgets(array(
     'email' => new sfWidgetFormInputText(), 
     'name' => new sfWidgetFormInputText(), 
     //.... 
     'city' => new sfWidgetFormInputText(), 
     'street' => new sfWidgetFormInputText(), 
     //.... 
     'bill_check' => new sfWidgetFormInputCheckbox(), 
     'bill_name' => new sfWidgetFormInputText(), 
     'bill_city' => new sfWidgetFormInputText(), 
     'bill_street' => new sfWidgetFormInputText(), 
     //.... 
    )); 
    $this->widgetSchema['bill_check']->setOption('value_attribute_value', 1); 
    $this->setValidators(array(
     'email' => new sfValidatorEmail(), 
     'name' => new sfValidatorString(), 
     //... 
     'city' => new sfValidatorString(), 
     'street' => new sfValidatorString(), 
     //... 
     'bill_check' => new sfValidatorBoolean(), 
    )); 
    if (/** the most convetional solution to check 'bill_check' state */) { 
     $this->validatorSchema['bill_name'] = new sfValidatorString(); 
     $this->validatorSchema['bill_city'] = new sfValidatorString(); 
     $this->validatorSchema['bill_street'] = new sfValidatorString(); 
     //.... 
    } 
    $this->widgetSchema->setNameFormat('orderAddForm[%s]'); 
    } 
} 

덕분에, 올리버

답변

2

당신은 귀하의 회신에 대한 postValidator

public function configure() { 
    // your current code 
    $this->validatorSchema->setPostValidator(
    new sfValidatorCallback(array('callback' => array($this, 'checkOtherStuff'))) 
); 
} 

public function checkOtherStuff($validator, $values) 
{ 
    // $values is an array of POSTed values 
    if ($values['bill_check'] == 'something in here') 
    { 
    if ($values['bill_city'] == '' || $values['bill_street'] == '') { 
     throw new sfValidatorError($validator, 'You must complete all fields'); 
    } 
    } 
    // bill_check is correct, return the clean values 
    return $values; 
} 

Blog article on the subject here

+0

하이 @ManseUK, THX를 사용할 수 있습니다. 그래서 내가 원하는대로 상태에 놓을 방법이 없다고 생각합니까? 왜냐하면 귀하의 경우에는 각 필드에 sfValidator (및 기능)를 어떻게 사용할 수 있는지 알지 못하기 때문입니다. –

관련 문제