* @package tao */ class tao_helpers_form_validators_Length extends tao_helpers_form_Validator { public function setOptions(array $options) { parent::setOptions($options); if ($this->hasOption('min') && $this->hasOption('max')) { $this->setMessage(__('Invalid field length') . " (minimum " . $this->getOption('min') . ", maximum " . $this->getOption('max') . ")"); } elseif ($this->hasOption('min') && !$this->hasOption('max')) { $this->setMessage(__('This field is too short') . " (minimum " . $this->getOption('min') . ")"); } elseif (!$this->hasOption('min') && $this->hasOption('max')) { $this->setMessage(__('This field is too long') . " (maximum " . $this->getOption('max') . ")"); } else { throw new Exception("Please set 'min' and/or 'max' options!"); } } /** * Short description of method evaluate * * @access public * @author Joel Bout, * @param values * @return boolean */ public function evaluate($values) { $returnValue = (bool) false; $returnValue = true; $values = is_array($values) ? $values : [$values]; foreach ($values as $value) { if ($this->hasOption('min') && mb_strlen($value) < $this->getOption('min')) { if ($this->hasOption('allowEmpty') && $this->getOption('allowEmpty') && empty($value)) { continue; } else { $returnValue = false; break; } } if ($this->hasOption('max') && mb_strlen($value) > $this->getOption('max')) { $returnValue = false; break; } } return (bool) $returnValue; } }