示例#1
0
 /**
  * Executes the validation
  */
 public function validate(Validation $validation, $field)
 {
     $value = $validation->getValue($field);
     if ($this->isSetOption("allowEmpty") && empty($value)) {
         return true;
     }
     /**
      * A domain is an array with a list of valid values
      */
     $domain = $this->getOption("domain");
     if (!is_array($domain)) {
         throw new Exception("Option 'domain' must be an array");
     }
     $strict = false;
     if ($this->isSetOption("strict")) {
         $strict = $this->getOption("strict");
     }
     /**
      * Check if the value is contained by the array
      */
     if (!in_array($value, $domain, $strict)) {
         $label = $this->getOption("label");
         if (empty($label)) {
             $label = $validation->getLabel($field);
         }
         $message = $this->getOption("message");
         $replacePairs = array(':field' => $label, ':domain' => join(', ', $domain));
         if (empty($message)) {
             $message = $validation->getDefaultMessage("InclusionIn");
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "InclusionIn"));
         return false;
     }
     return true;
 }
示例#2
0
 /**
  * Executes the validation
  */
 public function validate(Validation $validation, $field)
 {
     /**
      * Regular expression is set in the option 'pattern'
      * Check if the value match using preg_match in the PHP userland
      */
     $matches = null;
     $value = $validation->getValue($field);
     if ($this->isSetOption("allowEmpty") && empty($value)) {
         return true;
     }
     if (preg_match($this->getOption("pattern"), $value, $matches)) {
         $failed = $matches[0] != $value;
     } else {
         $failed = true;
     }
     if ($failed === true) {
         $label = $this->getOption("label");
         if (empty($label)) {
             $label = $validation->getLabel($field);
         }
         $message = $this->getOption("message");
         $replacePairs = array(':field' => $label);
         if (empty($message)) {
             $message = $validation->getDefaultMessage("Regex");
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "Regex"));
         return false;
     }
     return true;
 }
示例#3
0
 /**
  * Executes the validation
  */
 public function validate(Validation $validation, $field)
 {
     if ($validation->getValue($field) != $this->getOption("accepted")) {
         $label = $this->getOption("label");
         if (empty($label)) {
             $label = $validation->getLabel($field);
         }
         $message = $this->getOption("message");
         $replacePairs = array(':field' => $label);
         if (empty($message)) {
             $message = $validation->getDefaultMessage("Identical");
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "Identical"));
         return false;
     }
     return true;
 }
示例#4
0
 /**
  * Executes the validation
  */
 public function validate(Validation $validation, $field)
 {
     $value = $validation->getValue($field);
     if ($value === null || $value === "") {
         $label = $this->getOption("label");
         if (empty($label)) {
             $label = $validation->getLabel($field);
         }
         $message = $this->getOption('message');
         $replacePairs = array(':field' => $label);
         if (empty($message)) {
             $message = $validation->getDefaultMessage("PresenceOf");
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "PresenceOf"));
         return false;
     }
     return true;
 }
示例#5
0
 /**
  * Executes the validation
  */
 public function validate(Validation $validation, $field)
 {
     $value = $validation->getValue($field);
     if ($this->isSetOption("allowEmpty") && empty($value)) {
         return true;
     }
     if (!preg_match('/^-?\\d+\\.?\\d*$/', $value)) {
         $label = $this->getOption("label");
         if (empty($label)) {
             $label = $validation->getLabel($field);
         }
         $message = $this->getOption("message");
         $replacePairs = array(':field' => $label);
         if (empty($message)) {
             $message = $validation->getDefaultMessage("Numericality");
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "Numericality"));
         return false;
     }
     return true;
 }
示例#6
0
 /**
  * Executes the validation
  */
 public function validate(Validation $validation, $field)
 {
     $value = $validation->getValue($field);
     if ($this->isSetOption('allowEmpty') && empty($value)) {
         return true;
     }
     if (!ctype_alnum($value)) {
         $label = $this->getOption('label');
         if (empty($label)) {
             $label = $validation->getLabel($field);
         }
         $message = $this->getOption('message');
         $replacePairs = array(':field' => $label);
         if (empty($message)) {
             $message = $validation->getDefaultMessage('Alnum');
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, 'Alnum'));
         return false;
     }
     return true;
 }
示例#7
0
 /**
  * Executes the validation
  */
 public function validate(Validation $validation, $field)
 {
     $value = $validation->getValue($field);
     if ($this->isSetOption("allowEmpty") && empty($value)) {
         return true;
     }
     if (!ctype_digit($value)) {
         $label = $this->getOption("label");
         if (empty($label)) {
             $label = $validation->getLabel($field);
         }
         $message = $this->getOption("message");
         $replacePairs = array(':field' => $label);
         if (empty($message)) {
             $message = $validation->getDefaultMessage("Digit");
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "Digit"));
         return false;
     }
     return true;
 }
示例#8
0
 /**
  * Executes the validation
  */
 public function validate(Validation $validation, $field)
 {
     $value = $validation->getValue($field);
     if ($this->isSetOption('allowEmpty') && empty($value)) {
         return true;
     }
     if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
         $label = $this->getOption('label');
         if (empty($label)) {
             $label = $validation->getLabel($field);
         }
         $message = $this->getOption('message');
         $replacePairs = array(':field' => $label);
         if (empty($message)) {
             $message = $validation->getDefaultMessage('Email');
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, 'Email'));
         return false;
     }
     return true;
 }
示例#9
0
 /**
  * Executes the validation
  */
 public function validate(Validation $validation, $field)
 {
     $value = $validation->getValue($field);
     $minimum = $this->getOption("minimum");
     $maximum = $this->getOption("maximum");
     if ($this->isSetOption("allowEmpty") && empty($value)) {
         return true;
     }
     if ($value < $minimum || $value > $maximum) {
         $label = $this->getOption("label");
         if (empty($label)) {
             $label = $validation->getLabel($field);
         }
         $message = $this->getOption("message");
         $replacePairs = array(':field' => $label, ':min' => $minimum, ':max' => $maximum);
         if (empty($message)) {
             $message = $validation->getDefaultMessage("Between");
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "Between"));
         return false;
     }
     return true;
 }
示例#10
0
 /**
  * Executes the validation
  */
 public function validate(Validation $validation, $field)
 {
     $fieldWith = $this->getOption("with");
     $value = $validation->getValue($field);
     $valueWith = $validation->getValue($fieldWith);
     if (!$this->compare($value, $valueWith)) {
         $label = $this->getOption("label");
         if (empty($label)) {
             $label = $validation->getLabel($field);
         }
         $labelWith = $this->getOption("labelWith");
         if (empty($labelWith)) {
             $labelWith = $validation->getLabel($fieldWith);
         }
         $message = $this->getOption("message");
         $replacePairs = array(':field' => $label, ':with' => $labelWith);
         if (empty($message)) {
             $message = $validation->getDefaultMessage("Confirmation");
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "Confirmation"));
         return false;
     }
     return true;
 }
示例#11
0
 /**
  * Executes the validation
  */
 public function validate(Validation $validation, $field)
 {
     /**
      * At least one of 'min' or 'max' must be set
      */
     $isSetMin = $this->isSetOption("min");
     $isSetMax = $this->isSetOption("max");
     if (!$isSetMin && !$isSetMax) {
         throw new Exception("A minimum or maximum must be set");
     }
     $value = $validation->getValue($field);
     if ($this->isSetOption("allowEmpty") && empty($value)) {
         return true;
     }
     $label = $this->getOption("label");
     if (empty($label)) {
         $label = $validation->getLabel($field);
     }
     /**
      * Check if mbstring is available to calculate the correct length
      */
     if (function_exists("mb_strlen")) {
         $length = mb_strlen($value);
     } else {
         $length = strlen($value);
     }
     /**
      * Maximum length
      */
     if ($isSetMax) {
         $maximum = $this->getOption("max");
         if ($length > $maximum) {
             /**
              * Check if the developer has defined a custom message
              */
             $message = $this->getOption("messageMaximum");
             $replacePairs = array(':field' => $label, ':max' => $maximum);
             if (empty($message)) {
                 $message = $validation->getDefaultMessage("TooLong");
             }
             $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "TooLong"));
             return false;
         }
     }
     /**
      * Minimum length
      */
     if ($isSetMin) {
         $minimum = $this->getOption("min");
         if ($length < $minimum) {
             /**
              * Check if the developer has defined a custom message
              */
             $message = $this->getOption("messageMinimum");
             $replacePairs = array(':field' => $label, ':min' => $minimum);
             if (empty($message)) {
                 $message = $validation->getDefaultMessage("TooShort");
             }
             $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "TooShort"));
             return false;
         }
     }
     return true;
 }
示例#12
0
 /**
  * Executes the validation
  */
 public function validate(Validation $validation, $field)
 {
     $value = $validation->getValue($field);
     $label = $this->getOption("label");
     if (empty($label)) {
         $label = $validation->getLabel($field);
     }
     // Upload is larger than PHP allowed size (post_max_size or upload_max_filesize)
     if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' && empty($_POST) && empty($_FILES) && $_SERVER['CONTENT_LENGTH'] > 0 || isset($value["error"]) && $value["error"] === UPLOAD_ERR_INI_SIZE) {
         $message = $this->getOption("messageIniSize");
         $replacePairs = array(':field' => $label);
         if (empty($message)) {
             $message = $validation->getDefaultMessage("FileIniSize");
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "FileIniSize"));
         return false;
     }
     if ($this->isSetOption("allowEmpty") && (empty($value) || isset($value["error"]) && $value["error"] === UPLOAD_ERR_NO_FILE)) {
         return true;
     }
     if (!isset($value["error"]) || !isset($value["tmp_name"]) || $value["error"] !== UPLOAD_ERR_OK || !is_uploaded_file($value["tmp_name"])) {
         $message = $this->getOption("messageEmpty");
         $replacePairs = array(':field' => $label);
         if (empty($message)) {
             $message = $validation->getDefaultMessage("FileEmpty");
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "FileEmpty"));
         return false;
     }
     if (!isset($value["name"]) || !isset($value["type"]) || !isset($value["size"])) {
         $message = $this->getOption("messageValid");
         $replacePairs = array(':field' => $label);
         if (empty($message)) {
             $message = $validation->getDefaultMessage("FileValid");
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "FileValid"));
         return false;
     }
     if ($this->isSetOption("maxSize")) {
         $byteUnits = array("B" => 0, "K" => 10, "M" => 20, "G" => 30, "T" => 40, "KB" => 10, "MB" => 20, "GB" => 30, "TB" => 40);
         $maxSize = $this->getOption("maxSize");
         $matches = NULL;
         $unit = "B";
         preg_match('/^([0-9]+(?:\\.[0-9]+)?)(' . implode('|', array_keys($byteUnits)) . ')?$/Di', $maxSize, $matches);
         if (isset($matches[2])) {
             $unit = $matches[2];
         }
         $bytes = floatval($matches[1]) * pow(2, $byteUnits[$unit]);
         if (floatval($value["size"]) > floatval($bytes)) {
             $message = $this->getOption("messageSize");
             $replacePairs = array(':field' => $label, ':max' => $maxSize);
             if (empty($message)) {
                 $message = $validation->getDefaultMessage("FileSize");
             }
             $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "FileSize"));
             return false;
         }
     }
     if ($this->isSetOption("allowedTypes")) {
         $types = $this->getOption("allowedTypes");
         if (!is_array($types)) {
             throw new Exception("Option 'allowedTypes' must be an array");
         }
         if (function_exists("finfo_open")) {
             $tmp = finfo_open(FILEINFO_MIME_TYPE);
             $mime = finfo_file($tmp, $value["tmp_name"]);
             finfo_close($tmp);
         } else {
             $mime = $value["type"];
         }
         if (!in_array($mime, $types)) {
             $message = $this->getOption("messageType");
             $replacePairs = array(':field' => $label, ':types' => join(', ', $types));
             if (empty($message)) {
                 $message = $validation->getDefaultMessage("FileType");
             }
             $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "FileType"));
             return false;
         }
     }
     if ($this->isSetOption("minResolution") || $this->isSetOption("maxResolution")) {
         $tmp = $getimagesize($value["tmp_name"]);
         $width = $tmp[0];
         $height = $tmp[1];
         if ($this->isSetOption("minResolution")) {
             $minResolution = explode("x", $this->getOption("minResolution"));
             $minWidth = $minResolution[0];
             $minHeight = $minResolution[1];
         } else {
             $minWidth = 1;
             $minHeight = 1;
         }
         if ($width < $minWidth || $height < $minHeight) {
             $message = $this->getOption("messageMinResolution");
             $replacePairs = array(':field' => $label, ':min' => $this->getOption("minResolution"));
             if (empty($message)) {
                 $message = $validation->getDefaultMessage("FileMinResolution");
             }
             $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "FileMinResolution"));
             return false;
         }
         if ($this->isSetOption("maxResolution")) {
             $maxResolution = explode("x", $this->getOption("maxResolution"));
             $maxWidth = $maxResolution[0];
             $maxHeight = $maxResolution[1];
             if ($width > $maxWidth || $height > $maxHeight) {
                 $message = $this->getOption("messageMaxResolution");
                 $replacePairs = array(':field' => $label, ':max' => $this->getOption("maxResolution"));
                 if (empty($message)) {
                     $message = $validation->getDefaultMessage("FileMaxResolution");
                 }
                 $validation->appendMessage(new Message(strtr($message, $replacePairs), $field, "FileMaxResolution"));
                 return false;
             }
         }
     }
     return true;
 }