/** * @see sfValidatorBase */ protected function doClean($value) { $value = $this->convertToEnglishNumber($value); if (!preg_match('#^[\\d\\.]+$#', $value)) { throw new sfValidatorError($this, 'invalid', array('value' => $value)); } return parent::doClean($value); }
/** * @see sfValidatorFile */ protected function doClean($value) { $clean = parent::doClean($value); if ($clean == 0) { throw new sfValidatorError($this, 'not_nullity'); } return $clean; }
protected function configure($options = array(), $messages = array()) { parent::configure($options, $messages); $this->setMessage('required', isset($messages['required']) ? $messages['required'] : 'Obrigatório.'); $this->setMessage('invalid', isset($messages['invalid']) ? $messages['invalid'] : 'Preenchimento incorreto.'); $this->setMessage('min', isset($messages['min']) ? $messages['min'] : 'O valor deve ser maior ou igual a %min%.'); $this->setMessage('max', isset($messages['max']) ? $messages['max'] : 'O valor deve ser menor ou igual a %max%.'); }
protected function doClean($value) { if (preg_match_all('/[\\.,]/', $value, $matches) > 1) { throw new sfValidatorError($this, 'Only one decimal separator is allowed'); } // Translate to US-standard $pre_clean = preg_replace('/,/', '.', $value); $post_clean = parent::doClean($pre_clean); return $post_clean; }
/** * @see sfValidatorBase */ protected function doClean($value) { try { $value = sfNumberFormatPlus::getNumber($value, $this->getOption('culture')); } catch (Exception $e) { //print_r($e); throw new sfValidatorError($this, 'format', array('value' => $value)); } return parent::doClean($value); }
/** * @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; }
/** * @see sfValidatorFile */ protected function doClean($value) { $clean = parent::doClean($value['montant']); switch ($value['state']) { case 'debit': $clean = abs($clean) * -1; break; case 'credit': $clean = abs($clean); break; } return $clean; }
<?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');