/**
  * @see sfValidatorString
  */
 protected function doClean($value)
 {
     // clean by the parent
     $clean = parent::doClean($value);
     $numVal = new sfValidatorNumber(array('required' => false, 'min' => $this->getOption('min'), 'max' => $this->getOption('max')));
     $numVal->clean($value);
     // get the option
     $separator = $this->getOption('separator');
     $max_left = $this->getOption('max_left');
     $max_right = $this->getOption('max_right');
     // build the regexp to test
     $pattern = '/^\\d{1,' . $max_left . '}';
     $pattern .= '(\\' . $separator;
     $pattern .= '\\d{0,' . $max_right . '})?$/';
     // do the test
     if (!preg_match($pattern, $clean)) {
         throw new sfValidatorError($this, 'invalid', array('max_right' => $this->getOption('max_right'), 'value' => $value));
     }
     // return the $clean value
     return $clean;
 }
Example #2
0
<?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(13);
$v = new sfValidatorNumber();
// ->clean()
$t->diag('->clean()');
$t->is($v->clean(12.3), 12.3, '->clean() returns the numbers unmodified');
$t->is($v->clean('12.3'), 12.3, '->clean() converts strings to numbers');
$t->is($v->clean(12.12345678901234), 12.12345678901234, '->clean() returns the numbers unmodified');
$t->is($v->clean('12.12345678901234'), 12.12345678901234, '->clean() converts strings to numbers');
try {
    $v->clean('not a float');
    $t->fail('->clean() throws a sfValidatorError if the value is not a number');
} catch (sfValidatorError $e) {
    $t->pass('->clean() throws a sfValidatorError if the value is not a number');
}
$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.4);
    $t->fail('"max" option set the maximum number allowed');