2012-11-20 59 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(以及它的功能)。 –