1

bill_ceck投稿パラメーターに応じて請求先住所を検証するにはどうすればよいですか?

投稿の検証 ( http://symfony.com/legacy/doc/cookbook/1_2/en/conditional-validator ) を確認しましたが、OR ではなく AND 検証のように思えます。

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]');
  }
}

ありがとう、オリバー

4

1 に答える 1

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;
}

この件に関するブログ記事はこちら

于 2012-11-20T16:33:11.893 に答える