Ejemplo n.º 1
0
 public function bind($params)
 {
     if ('input' === $params['form_type'] || 'textarea' === $params['form_type']) {
         $validatorArgs = array('required' => false, 'trim' => true);
         $validatorMin = new sfValidatorInteger($validatorArgs);
         $validatorMax = new sfValidatorInteger($validatorArgs);
         if ('integer' !== $params['value_type']) {
             $validatorMin->setOption('min', 0);
             $validatorMax->setOption('min', 1);
         }
         $this->setValidator('value_min', $validatorMin);
         $this->setValidator('value_max', $validatorMax);
     } elseif ('date' === $params['form_type']) {
         $validatorArgs = array('required' => false, 'trim' => true, 'date_format' => '/^(?P<year>\\d{4})\\/(?P<month>\\d{1,2})\\/(?P<day>\\d{1,2})$/', 'date_output' => 'Y/m/d', 'date_format_error' => 'YYYY/MM/DD');
         $validatorMin = new opValidatorDate($validatorArgs);
         $validatorMax = new opValidatorDate($validatorArgs);
         $this->setValidator('value_min', $validatorMin);
         $this->setValidator('value_max', $validatorMax);
     } elseif ($params['value_min'] || $params['value_max']) {
         throw new sfValidatorError($validator, 'invalid');
     }
     $this->mergePostValidator(new sfValidatorCallback(array('callback' => array($this, 'compareMinAndMax')), array('invalid' => 'Value must be greater than or equal to Minimum value.')));
     return parent::bind($params);
 }
Ejemplo n.º 2
0
    $v->clean('not an integer');
    $t->fail('->clean() throws a sfValidatorError if the value is not an integer');
    $t->skip('', 1);
} catch (sfValidatorError $e) {
    $t->pass('->clean() throws a sfValidatorError if the value is not an integer');
    $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError');
}
try {
    $v->clean(12.3);
    $t->fail('->clean() throws a sfValidatorError if the value is not an integer');
    $t->skip('', 1);
} catch (sfValidatorError $e) {
    $t->pass('->clean() throws a sfValidatorError if the value is not an integer');
    $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError');
}
$v->setOption('required', false);
$t->ok($v->clean(null) === null, '->clean() returns null for null values');
$v->setOption('max', 2);
$t->is($v->clean(1), 1, '->clean() checks the maximum number allowed');
try {
    $v->clean(3);
    $t->fail('"max" option set the maximum number allowed');
    $t->skip('', 1);
} catch (sfValidatorError $e) {
    $t->pass('"max" option set the maximum number allowed');
    $t->is($e->getCode(), 'max', '->clean() throws a sfValidatorError');
}
$v->setMessage('max', 'Too large');
try {
    $v->clean(5);
    $t->fail('"max" error message customization');