Dondariadmin (トーク | 投稿記録) (→PostValidator内に複数のバリデータを設定するには) |
Dondariadmin (トーク | 投稿記録) |
||
57行目: | 57行目: | ||
} | } | ||
} | } | ||
+ | |||
+ | == 先行するチェックがエラーの場合、バリデートをそこでやめる == | ||
+ | |||
+ | * チェック項目が2つあり、1つ目がエラーなら、2つ目はチェックしないほうが良い場合。 | ||
+ | |||
+ | $this->getValidatorSchema()->setPostValidator(new sfValidatorAnd(array( | ||
+ | new sfValidatorSchemaCompare('age_from', '<=', 'age_to'), | ||
+ | new sfValidatorSchemaCompare('work_length_from', '<=', 'work_length_to'), | ||
+ | ), array('halt_on_error' => true) | ||
+ | )); |
class hogeForm extends BaseForm { public function configure() { $this->setWidgets(array( 'age_from' => new sfWidgetFormInputText(), 'age_to' => new sfWidgetFormInputText(), )); $this->getValidatorSchema()->setPostValidator( // '<='等の比較演算子文字列は、sfValidatorSchemaCompareクラス内に定数宣言もありますが、このほうが分かりやすいように思います。 new sfValidatorSchemaCompare('age_from', '<=', 'age_to') ); $this->getWidgetSchema()->setNameFormat('hogeinput[%s]'); $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema); } }
class hogeForm extends BaseForm { public function configure() { $this->setWidgets(array( 'age_from' => new sfWidgetFormInputText(), 'age_to' => new sfWidgetFormInputText(), 'work_length_from' => new sfWidgetFormInputText(), 'work_length_to' => new sfWidgetFormInputText(), )); $this->setValidators(array( 'age_from' => new sfValidatorInteger(), 'age_to' => new sfValidatorInteger(), 'work_length_from' => new sfValidatorInteger(), 'work_length_to' => new sfValidatorInteger(), )); $this->getValidatorSchema()->setPostValidator(new sfValidatorAnd(array( new sfValidatorSchemaCompare('age_from', '<=', 'age_to'), new sfValidatorSchemaCompare('work_length_from', '<=', 'work_length_to'), ))); $this->getWidgetSchema()->setNameFormat('hogeinput[%s]'); $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema); } }
$this->getValidatorSchema()->setPostValidator(new sfValidatorAnd(array( new sfValidatorSchemaCompare('age_from', '<=', 'age_to'), new sfValidatorSchemaCompare('work_length_from', '<=', 'work_length_to'), ), array('halt_on_error' => true) ));Tweet