protected function checkRange($range = array())
 {
     // checking single values
     if (isset($range['date_from'])) {
         $v = new sfValidatorDate();
         $range['date_from'] = $v->clean($range['date_from']);
     }
     if (isset($range['date_to'])) {
         $v = new sfValidatorDate();
         $range['date_to'] = $v->clean($range['date_to']);
     }
     if (isset($range['kilometers_from'])) {
         $v = new sfValidatorInteger(array('min' => 0));
         $range['kilometers_from'] = $v->clean($range['kilometers_from']);
     }
     if (isset($range['kilometers_to'])) {
         $v = new sfValidatorInteger(array('min' => 0));
         $range['kilometers_to'] = $v->clean($range['kilometers_to']);
     }
     // checking ranges
     if (isset($range['date_from']) && isset($range['kilometers_from'])) {
         throw new sfException('Only one between "date_from" and "kilometers_from" can be set.');
     }
     if (isset($range['date_to']) && isset($range['kilometers_to'])) {
         throw new sfException('Only one between "date_to" and "kilometers_to" can be set.');
     }
     $this->log('Range: ');
     if (isset($range['date_from'])) {
         $this->log('  - From ' . $range['date_from']);
     }
     if (isset($range['kilometers_from'])) {
         $this->log('  - From ' . $range['kilometers_from'] . ' km');
     }
     if (isset($range['date_to'])) {
         $this->log('  - To ' . $range['date_to']);
     }
     if (isset($range['kilometers_to'])) {
         $this->log('  - To ' . $range['kilometers_to'] . ' km');
     }
     if (!isset($range['date_from']) && !isset($range['kilometers_from'])) {
         $range['kilometers_from'] = 0;
         $this->log('  - From 0 km');
     }
     if (!isset($range['date_to']) && !isset($range['kilometers_to'])) {
         $range['date_to'] = date('Y-m-d');
         $this->log('  - To ' . date('Y-m-d'));
     }
     $this->log(' ');
     return $range;
 }
 public static function advancedValidator($validator, $values)
 {
     if ($values['form_type'] === 'input' || $values['form_type'] === 'textarea') {
         $validator = new sfValidatorInteger(array('required' => false));
         $values['value_min'] = $validator->clean($values['value_min']);
         $values['value_max'] = $validator->clean($values['value_max']);
     } elseif ($values['form_type'] === 'date') {
         $validator = new opValidatorDate(array('required' => false));
         $validator->clean($values['value_min']);
         $validator->clean($values['value_max']);
     } elseif ($values['value_min'] || $values['value_max']) {
         throw new sfValidatorError($validator, 'invalid');
     }
     return $values;
 }
Beispiel #3
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);
 }
<?php

/*
 * This file is part of the symfony package.
 * (c) Fabien Potencier <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require_once dirname(__FILE__) . '/../../bootstrap/unit.php';
$t = new lime_test(15);
$v = new sfValidatorInteger();
// ->clean()
$t->diag('->clean()');
$t->is($v->clean(12), 12, '->clean() returns the numbers unmodified');
$t->is($v->clean('12'), 12, '->clean() converts strings to integers');
try {
    $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');
 protected function doClean($value)
 {
     // Translate to US-standard
     $pre_clean = preg_replace('/\\./', '', $value);
     return parent::doClean($pre_clean);
 }
 /**
  * @see sfValidatorBase
  */
 protected function doClean($value)
 {
     $clean = intval($value * 100);
     return parent::doClean($clean);
 }