/** * @see sfValidatorBase */ protected function doClean($value) { $clean = (string) $value; $this->setMessage('invalid', '2nd Selected Age Lager Than The 1st Selected Age.'); $compareValidator = new sfValidatorSchemaCompare('value1', sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'value2', array('throw_global_error' => true), array('invalid' => $this->getMessage('invalid'))); $compareValidator->clean($value); return $clean; }
/** * @see sfValidatorBase */ protected function doClean($value) { $value['from'] = $this->getOption('from_date')->clean(isset($value['from']) ? $value['from'] : null); $value['to'] = $this->getOption('to_date')->clean(isset($value['to']) ? $value['to'] : null); if ($value['from'] && $value['to']) { $v = new sfValidatorSchemaCompare('from', sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'to', array('throw_global_error' => true), array('invalid' => $this->getMessage('invalid'))); $v->clean($value); } return $value; }
/** * Clean input value * * @param mixed $value Input value * @return mixed Cleaned value */ protected function doClean($value) { $minField = $this->getOption('min_field'); $maxField = $this->getOption('max_field'); $value[$minField] = $this->getOption('min')->clean(isset($value[$minField]) ? $value[$minField] : null); $value[$maxField] = $this->getOption('max')->clean(isset($value[$maxField]) ? $value[$maxField] : null); if ($value[$minField] && $value[$maxField]) { $v = new sfValidatorSchemaCompare($minField, sfValidatorSchemaCompare::LESS_THAN_EQUAL, $maxField, array('throw_global_error' => true), array('invalid' => $this->getMessage('invalid'))); $v->clean($value); } return $value; }
/** * @see sfValidatorBase */ protected function doClean($value) { $value['from'] = $this->getOption('from_date')->clean(isset($value['from']) ? $value['from'] : null); $value['to'] = $this->getOption('to_date')->clean(isset($value['to']) ? $value['to'] : null); if ($days = $this->getOption('max_days_in_range')) { $duration = (strtotime($value['to']) - strtotime($value['from'])) / 86400; if ($duration > $days) { throw new sfValidatorError($this, 'max', array('max' => $days)); } } if ($value['from'] && $value['to']) { $v = new sfValidatorSchemaCompare('from', sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'to', array('throw_global_error' => true), array('invalid' => $this->getMessage('invalid'))); $v->clean($value); } return $value; }
foreach (array(true, false) as $globalError) { $v->setOption('throw_global_error', $globalError); try { $v->clean($values[0]); $t->fail('->clean() throws an sfValidatorError if the value is the comparison failed'); $t->skip('', 1); } catch (sfValidatorError $e) { $t->pass('->clean() throws an sfValidatorError if the value is the comparison failed'); $t->is($e->getCode(), $globalError ? 'invalid' : 'left [invalid]', '->clean() throws a sfValidatorError'); } } } try { $v->clean('foo'); $t->fail('->clean() throws an InvalidArgumentException exception if the first argument is not an array of value'); } catch (InvalidArgumentException $e) { $t->pass('->clean() throws an InvalidArgumentException exception if the first argument is not an array of value'); } $v = new sfValidatorSchemaCompare('left', 'foo', 'right'); try { $v->clean(array()); $t->fail('->clean() throws an InvalidArgumentException exception if the operator does not exist'); } catch (InvalidArgumentException $e) { $t->pass('->clean() throws an InvalidArgumentException exception if the operator does not exist'); } // ->asString() $t->diag('->asString()'); $v = new sfValidatorSchemaCompare('left', sfValidatorSchemaCompare::EQUAL, 'right'); $t->is($v->asString(), 'left == right', '->asString() returns a string representation of the validator'); $v = new sfValidatorSchemaCompare('left', sfValidatorSchemaCompare::EQUAL, 'right', array(), array('required' => 'This is required.')); $t->is($v->asString(), 'left ==({}, { required: \'This is required.\' }) right', '->asString() returns a string representation of the validator');
/** * IsConfirm: true のフィールド用の Post Validator * * 元フィールドと _confirm フィールドの個別バリデーションもこの中でおこない、 * エラーにならなかった場合のみ両者を比較することで、エラーが重複して表示されるのを防ぐ */ public function postValidateConfirmField($validator, $values, $arguments = array()) { $name = $arguments['name']; $fieldValidator = $arguments['validator']; // バリデーションエラー時のフォーム画面に「必須項目マーク(*)」を表示するためバリデーターを元に戻す $this->validatorSchema[$name] = $this->validatorSchema[$name . '_confirm'] = $fieldValidator; // 元フィールドのバリデーション try { $values[$name] = $fieldValidator->clean($values[$name]); } catch (sfValidatorError $e) { throw new sfValidatorErrorSchema($validator, array($name => $e)); } // _confirm フィールドのバリデーション try { $values[$name . '_confirm'] = $fieldValidator->clean($values[$name . '_confirm']); } catch (sfValidatorError $e) { // _confirm だけエラーになる場合、2つのフィールドの値は一致していないので、 // sfValidatorSchemaCompare と同じ invalid エラーの例外を投げる throw new sfValidatorErrorSchema($validator, array($name . '_confirm' => new sfValidatorError($validator, 'invalid'))); } // 2つのフィールドが共にエラーでない場合のみ値を比較する // validator の clean() は値を変更することがあるため、clean() 後の値を比較する $compareValidator = new sfValidatorSchemaCompare($name . '_confirm', '===', $name); $values = $compareValidator->clean($values); return $values; }