【问题标题】:symfony 1.4 conditional validation of shipping addresssymfony 1.4 发货地址的条件验证
【发布时间】:2012-11-21 00:33:11
【问题描述】:

如何根据bill_ceck post 参数验证账单地址?

我查看了帖子验证 (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]');
  }
}

谢谢, 奥利弗

【问题讨论】:

    标签: symfony-1.4 symfony-forms


    【解决方案1】:

    您可以使用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

    【讨论】:

    • 嗨@ManseUK,谢谢您的回复。所以你认为我没有办法按照我的意愿在 if 条件下添加一些东西?因为在您的情况下,我不知道如何在每个字段上使用 sfValidator (及其功能)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多