Beispiel #1
0
 /**
  * Whether the input is valid or not.
  *
  * @param   bool   $revalid    Re-valid or not.
  * @param   mixed  $value      Value to test.
  * @return  bool
  */
 public function isValid($revalid = false, &$value)
 {
     if (false === $revalid && null !== $this->_validity) {
         return $this->_validity;
     }
     $this->_validity = true;
     $type = strtolower($this->readAttribute('type'));
     if ((null === $value || '' === $value) && true === $this->attributeExists('required')) {
         $this->_validity = false;
         return Form::postValidation($this->_validity, $value, $this);
     }
     if (false !== strpos($value, "\n") || false !== strpos($value, "\r")) {
         $this->_validity = false;
         return Form::postValidation($this->_validity, $value, $this);
     }
     if (true === $this->attributeExists('pattern')) {
         $pattern = str_replace('#', '\\#', $this->readAttribute('pattern'));
         if (0 == @preg_match('#^' . $pattern . '$#u', $value, $_)) {
             $this->_validity = false;
             return Form::postValidation($this->_validity, $value, $this);
         }
     }
     if (true === $this->attributeExists('maxlength')) {
         $maxlength = intval($this->readAttribute('maxlength'));
         if (mb_strlen($value) > $maxlength) {
             $this->_validity = false;
             return Form::postValidation($this->_validity, $value, $this);
         }
     }
     if (true === $this->attributeExists('readonly')) {
         $this->_validity = $value === $this->readAttribute('value');
         return Form::postValidation($this->_validity, $value, $this);
     }
     switch ($type) {
         case 'hidden':
             $this->_validity = $value === $this->readAttribute('value');
             break;
         case 'color':
             $this->_validity = 0 !== preg_match(Realdom\Color::REGEX, $value, $ );
             break;
             // @TODO
         // @TODO
         case 'tel':
         case 'url':
         case 'datetime':
         case 'date':
         case 'month':
         case 'week':
         case 'time':
         case 'datetime-local':
         case 'file':
             $this->_validity = true;
             break;
         case 'email':
             $this->_validity = false !== filter_var($value, FILTER_VALIDATE_EMAIL);
             break;
         case 'number':
             $value = floatval($value);
             if (false === $this->attributeExists('min')) {
                 if (true === $this->attributeExists('max')) {
                     $max = floatval($this->readAttribute('max'));
                     if ($value > $max) {
                         $this->_validity = false;
                         break;
                     }
                 }
                 $this->_validity = true;
                 break;
             }
             $min = floatval($this->readAttribute('min'));
             if ($value < $min) {
                 $this->_validity = false;
                 break;
             }
             if (false === $this->attributeExists('max')) {
                 $this->_validity = true;
                 break;
             }
             $max = floatval($this->readAttribute('max'));
             if ($value > $max) {
                 $this->_validity = false;
                 break;
             }
             // @TODO step
             $this->_validity = true;
             break;
         case 'range':
             $value = floatval($value);
             $min = floatval($this->readAttribute('min') ?: 0);
             $max = floatval($this->readAttribute('max') ?: 100);
             if ($value < $min || $value > $max) {
                 $this->_validity = false;
             }
             $step = $this->getStep();
             if (false === $step) {
                 $this->_validity = false;
                 break;
             } elseif (true !== $step) {
                 // @TODO
                 //$this->_validity = $min % $step === $value % $step;
                 $this->_validity = true;
             }
             break;
         case 'checkbox':
             if (null === $value) {
                 $this->_validity = true;
             } elseif (!is_string($value)) {
                 $this->_validity = false;
             } else {
                 $this->_validity = $value === $this->readAttribute('value');
             }
             break;
         case 'radio':
             if (!is_string($value)) {
                 $this->_validity = false;
             } else {
                 $this->_validity = $value === $this->readAttribute('value');
             }
             break;
         case 'submit':
         case 'image':
         case 'reset':
         case 'button':
             $this->_validity = true;
             break;
         case 'text':
         case 'search':
         case 'password':
         default:
             $this->_validity = true;
     }
     return Form::postValidation($this->_validity, $value, $this);
 }
Beispiel #2
0
 /**
  * Whether the input is valid or not.
  *
  * @param   bool   $revalid    Re-valid or not.
  * @param   mixed  $value      Value to test.
  * @return  bool
  */
 public function isValid($revalid = false, $value)
 {
     if (false === $revalid && null !== $this->_validity) {
         return $this->_validity;
     }
     $this->_validity = true;
     if ((null === $value || '' === $value) && true === $this->attributeExists('required')) {
         $this->_validity = false;
         return Form::postValidation($this->_validity, $value, $this);
     }
     if (true === $this->attributeExists('maxlength')) {
         $maxlength = intval($this->readAttribute('maxlength'));
         if (mb_strlen($value) > $maxlength) {
             $this->_validity = false;
             return Form::postValidation($this->_validity, $value, $this);
         }
     }
     if (true === $this->attributeExists('readonly')) {
         $this->_validity = $value === $this->computeValue();
         return Form::postValidation($this->_validity, $value, $this);
     }
     return Form::postValidation($this->_validity, $value, $this);
 }