Example #1
0
 public function validate(\Phalcon\Validation $validator, $attribute)
 {
     $user = new \Models\User();
     if (!$user->isAuth($validator->getValue('login'), $validator->getValue($attribute))) {
         $validator->appendMessage(new \Phalcon\Validation\Message($this->getOption('message'), $attribute));
         return false;
     }
     return true;
 }
 /**
  * Executes the validation
  *
  * @param Phalcon\Validation $validator
  * @param string $attribute
  * @return boolean
  */
 public function validate(Validation $validator, $attribute)
 {
     $field = ['id' => $attribute, 'value' => $validator->getValue($attribute)];
     $withField = [];
     if ($this->getOption('with_field')) {
         $fieldId = $this->getOption('with_field');
         $withField = ['id' => $fieldId, 'value' => $validator->getValue($fieldId)];
     }
     if ($this->isEntityExist($field, $withField)) {
         $message = "Value must be unique";
         $validator->appendMessage(new Message($message, $attribute, 'Unique'));
         return false;
     }
     return true;
 }
 /**
  * @param \Phalcon\Validation $validation
  * @param string              $field
  *
  * @return boolean
  *
  * @throws \Phalcon\Validation\Exception
  */
 public function validate(\Phalcon\Validation $validation, $field)
 {
     if (!is_string($field)) {
         throw new \Phalcon\Validation\Exception("Field name must be a string");
     }
     $label = $this->getOption("label");
     if (empty($label)) {
         $label = $validation->getLabel($field);
     }
     $value = $validation->getValue($field);
     if ($this->isSetOption("allowEmpty") && empty($value)) {
         return true;
     }
     // http://stackoverflow.com/questions/1418423/the-hostname-regex
     if (!preg_match('/^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\\.?$/', $value)) {
         $message = $this->getOption("message");
         if (empty($message)) {
             $message = "Field :field is not a valid hostname";
         }
         $replacePairs = [":field" => $label];
         $validation->appendMessage(new \Phalcon\Validation\Message(strtr($message, $replacePairs), $field, "Hostname"));
         return false;
     }
     return true;
 }
Example #4
0
 public function validate(Validation $validator, $attribute)
 {
     $value = $validator->getValue($attribute);
     $message = $this->getOption('message');
     if (!$message) {
         $message = '您填写的密码格式有误';
     }
     if (preg_match('/^[a-zA-Z0-9\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\+\\|\\~]{8,32}$/', $value) == 0) {
         $message = '您填写的密码格式有误,密码只能包含8-32个英文大小写字母、数字和以下符号:!@#$%^&*()_+|~,且不能全部由字母或数字组成。';
         $validator->appendMessage(new Message($message, $attribute));
         return false;
     }
     if (preg_match_all('/[0-9]/', $value) == strlen($value)) {
         $message = '您填写的密码格式有误,密码不能只包含数字。';
         $validator->appendMessage(new Message($message, $attribute));
         return false;
     }
     if (preg_match_all('/[a-z]/', $value) == strlen($value)) {
         $message = '您填写的密码格式有误,密码不能只包含英文小写字母。';
         $validator->appendMessage(new Message($message, $attribute));
         return false;
     }
     if (preg_match_all('/[A-Z]/', $value) == strlen($value)) {
         $message = '您填写的密码格式有误,密码不能只包含英文大写字母。';
         $validator->appendMessage(new Message($message, $attribute));
         return false;
     }
     return true;
 }
 /**
  * Executes the validation
  *
  * @param Phalcon\Validation $validator
  * @param string $attribute
  * @return boolean
  */
 public function validate(\Phalcon\Validation $validator, $attribute)
 {
     $flag = true;
     $reqType = $validator->getValue($attribute);
     // 待校验的文件集合
     $fileType = $this->getOption('filetype');
     // 合法的文件类型集合
     $errorCode = $this->getOption('code');
     foreach ($reqType as $file) {
         $extArr = explode('.', $file);
         $ext = array_pop($extArr);
         if (!in_array($ext, $fileType)) {
             $flag = false;
             break;
         }
     }
     if (!$flag) {
         $message = $this->getOption('message');
         if (!$message) {
             $message = 'The filetype is not valid';
         }
         $validator->appendMessage(new Message($message, $attribute, 'filetype'));
         return false;
     }
     return true;
 }
Example #6
0
 /**
  * {@inheritdoc}
  *
  * @param Validation $validation
  * @param string     $attribute
  *
  * @return bool
  */
 public function validate(Validation $validation, $attribute)
 {
     $secret = $this->getOption('secret');
     $value = $validation->getValue($attribute);
     $request = $validation->getDI()->get('request');
     $remoteIp = $request->getClientAddress(false);
     if (!empty($value)) {
         $curl = curl_init(self::RECAPTCHA_URL);
         curl_setopt_array($curl, [CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => ['secret' => $secret, 'response' => $value, 'remoteip' => $remoteIp]]);
         $response = json_decode(curl_exec($curl), true);
         curl_close($curl);
     }
     if (empty($response['success'])) {
         $label = $this->getOption('label');
         if (empty($label)) {
             $label = $validation->getLabel($attribute);
         }
         $message = $this->getOption('message');
         $replacePairs = [':field', $label];
         if (empty($message) && !empty($response['error-codes'])) {
             $message = $this->messages[$response['error-codes']];
         }
         if (empty($message)) {
             $message = $validation->getDefaultMessage('ReCaptcha');
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $attribute, 'ReCaptcha'));
         return false;
     }
     return true;
 }
Example #7
0
 /**
  * {@inheritdoc}
  *
  * @param Validation $validation
  * @param string $attribute
  *
  * @return bool
  * @throws Exception
  */
 public function validate(Validation $validation, $attribute)
 {
     $value = $validation->getValue($attribute);
     $field = $this->getOption('label');
     if (empty($field)) {
         $validation->getLabel($attribute);
     }
     if (false === $this->hasOption('places')) {
         throw new Exception('A number of decimal places must be set');
     }
     if ($this->hasOption('digits')) {
         // Specific number of digits
         $digits = '{' . (int) $this->getOption('digits') . '}';
     } else {
         // Any number of digits
         $digits = '+';
     }
     if ($this->hasOption('point')) {
         $decimal = $this->getOption('point');
     } else {
         // Get the decimal point for the current locale
         list($decimal) = array_values(localeconv());
     }
     $result = (bool) preg_match(sprintf('#^[+-]?[0-9]%s%s[0-9]{%d}$#', $digits, preg_quote($decimal), $this->getOption('places')), $value);
     if (!$result) {
         $message = $this->getOption('message');
         $replacePairs = [':field' => $field];
         if (empty($message)) {
             $message = ':field must contain valid decimal value';
         }
         $validation->appendMessage(new Message(strtr($message, $replacePairs), $attribute, 'Decimal'));
         return false;
     }
     return true;
 }
Example #8
0
 /**
  * Executes the validation
  *
  * @param Phalcon\Validation $validator
  * @param string $attribute
  * @return boolean
  */
 public function validate(\Phalcon\Validation $validator, $attribute)
 {
     $flag = true;
     $value = $validator->getValue($attribute);
     $errorCode = $this->getOption('code');
     if (!is_array($value)) {
         $message = '参数必须是数组';
         $validator->appendMessage(new Message($message, $attribute, 'Nums'));
         return false;
     }
     $countVal = count($value);
     $ruleMin = $this->getOption('min');
     $ruleMax = $this->getOption('max');
     if ($ruleMin == $ruleMax) {
         if ($countVal < $ruleMin) {
             $flag = false;
         }
     } else {
         if ($countVal < $ruleMin || $countVal > $ruleMax) {
             $flag = false;
         }
     }
     if (!$flag) {
         $message = $this->getOption('message');
         if (!$message) {
             $message = 'The num is not valid';
         }
         $validator->appendMessage(new Message($message, $attribute, 'Nums'));
         return false;
     }
     return true;
 }
Example #9
0
 /**
  * Executes the validation
  *
  * @package     base-app
  * @version     2.0
  *
  * @param object $validation Phalcon\Validation
  * @param string $field field name
  *
  * @return boolean
  *
  * @throws \Phalcon\Validation\Exception
  */
 public function validate(\Phalcon\Validation $validation, $field)
 {
     $value = $validation->getValue($field);
     $model = $this->getOption("model");
     $attribute = $this->getOption("attribute");
     if (empty($model)) {
         throw new \Phalcon\Validation\Exception("Model must be set");
     }
     if (empty($attribute)) {
         $attribute = $field;
     }
     if ($except = $this->getOption('except')) {
         $number = $model::count(array($attribute . "=:value: AND " . $attribute . "!= :except:", "bind" => array("value" => $value, 'except' => $except)));
     } else {
         $number = $model::count(array($attribute . "=:value:", "bind" => array("value" => $value)));
     }
     if ($number) {
         $label = $this->getOption("label");
         if (empty($label)) {
             $label = $validation->getLabel($field);
             if (empty($label)) {
                 $label = $field;
             }
         }
         $message = $this->getOption("message");
         $replacePairs = array(":field" => $label);
         if (empty($message)) {
             $message = $validation->getDefaultMessage("Uniqueness");
         }
         $validation->appendMessage(new \Phalcon\Validation\Message(strtr($message, $replacePairs), $field, "Uniqueness"));
         return false;
     }
     return true;
 }
Example #10
0
 /**
  * Executes the validation
  * Note: Explicitly supply `conditions` and `bind` options if model PK is not `id`
  * @param mixed $validation
  * @param string $attribute
  * @return bool
  */
 public function validate(Validation $validation, $attribute)
 {
     $value = $validation->getValue($attribute);
     $model_name = $this->getOption('model', null);
     $conditions = $this->getOption('conditions', null);
     $bind = $this->getOption('bind', []);
     $show_messages = $this->getOption('show_messages', true);
     /** @var \Phalcon\Mvc\Model $model */
     $model = $this->getModel($model_name);
     if (is_null($conditions)) {
         if (is_null($value)) {
             return false;
         }
         $data = $model::findFirst(["id = ?0", "bind" => $value]);
     } else {
         $data = $model::findFirst(['conditions' => $conditions, 'bind' => $bind]);
     }
     if (!$data) {
         if ($show_messages) {
             $this->addMessageToValidation($validation, 'Invalid :field supplied', $attribute, 'Model');
         }
         return false;
     }
     return true;
 }
Example #11
0
 public function validate(Validation $validation, $attribute)
 {
     $value = $validation->getValue($attribute);
     $model = $this->getOption('model');
     $except = $this->getOption("except");
     if (empty($model)) {
         throw new \Exception("Model must be set");
     }
     if (empty($attribute)) {
         throw new \Exception("arrtibute must be set");
     }
     if ($except) {
         $number = $model::count(sprintf($attribute . " = %s AND " . $attribute . " != %s", $value, $except));
     } else {
         $number = $model::count(sprintf($attribute . " = %s", $value));
     }
     if (!$number) {
         $message = $this->getOption('message');
         if (empty($message)) {
             $message = '字段对应的值不存在';
         }
         $validation->appendMessage(new Message($message, $attribute, "Existence"));
         return false;
     }
     return true;
 }
Example #12
0
 public function validate(\Phalcon\Validation $validation, $field)
 {
     $password = $validation->getValue($field);
     $error = FALSE;
     if (strlen($password) == 0) {
         $error = 'You must provide a password.';
     }
     $strength = 0;
     /*** get the length of the password ***/
     $length = strlen($password);
     /*** check if password is not all lower case ***/
     if (strtolower($password) != $password) {
         $strength += 1;
     }
     /*** check if password is not all upper case ***/
     if (strtoupper($password) != $password) {
         $strength += 1;
     }
     /*** check string length is 8 -15 chars ***/
     if ($length >= 8 && $length <= 15) {
         $strength += 1;
     }
     /*** check if lenth is 16 - 35 chars ***/
     if ($length >= 16 && $length <= 35) {
         $strength += 2;
     }
     /*** check if length greater than 35 chars ***/
     if ($length > 35) {
         $strength += 3;
     }
     /*** get the numbers in the password ***/
     preg_match_all('/[0-9]/', $password, $numbers);
     $strength += count($numbers[0]);
     /*** check for special chars ***/
     preg_match_all('/[|!@#$%&*\\/=?,;.:\\-_+~^\\\\]/', $password, $specialchars);
     $strength += sizeof($specialchars[0]);
     /*** get the number of unique chars ***/
     $chars = str_split($password);
     $num_unique_chars = sizeof(array_unique($chars));
     $strength += $num_unique_chars * 2;
     /*** strength is a number 1-10; ***/
     $strength = $strength > 99 ? 99 : $strength;
     $strength = floor($strength / 10 + 1);
     $min_strength = \Phalcon\DI::getDefault()->getShared('config')->auth->password_strength;
     if ($error || $strength < $min_strength) {
         $message = $this->getOption("message");
         if (empty($message)) {
             if ($error) {
                 $message = 'Please enter a passsword!';
             } else {
                 $message = 'Weak password! Try longer with mix of upper & lower case and numbers';
             }
         }
         $validation->appendMessage(new \Phalcon\Validation\Message($message, $field, "Password"));
         return false;
     }
     return TRUE;
 }
 public function validate(\Phalcon\Validation $validator, $attribute)
 {
     $value = $validator->getValue($attribute);
     if (isset($value['error']) and isset($value['name']) and isset($value['type']) and isset($value['tmp_name']) and isset($value['size'])) {
         return true;
     }
     $validator->appendMessage(new Message('Некорректный файл.', $attribute, 'upload_valid'));
     return false;
 }
Example #14
0
 /**
  * Executes the validation
  *
  * @param mixed $validation
  * @param string $attribute
  * @return bool
  */
 public function validate(\Phalcon\Validation $validation, $attribute)
 {
     if (!$this->validateImei($validation->getValue($attribute))) {
         $this->addMessageToValidation($validation, 'Invalid :field supplied', $attribute, 'IMEINumber');
         return false;
     } else {
         return true;
     }
 }
Example #15
0
 public function validate(Validation $validator, $attribute)
 {
     $value = $validator->getValue($attribute);
     if (preg_match('/^[a-zA-Z\\x{4e00}-\\x{9fa5}][a-zA-Z\\x{4e00}-\\x{9fa5}_ ]{1,19}$/u', $value) == 0) {
         $validator->appendMessage(new Message('您填写的动物名格式有误,动物名只能包含2-20个英文大小写字母、汉字、数字、下划线和空格,且不能以数字、下划线和空格开头', $attribute));
         return false;
     }
     return true;
 }
Example #16
0
 /**
  * Value validation
  *
  * @param   \Phalcon\Validation $validation - validation object
  * @param   string $attribute - validated attribute
  * @return  bool
  * @throws  \Phalcon\Validation\Exception
  */
 public function validate(Validation $validation, $attribute)
 {
     if (!$this->hasOption('origField')) {
         throw new Exception('Original field must be set');
     }
     $allowEmpty = $this->getOption('allowEmpty');
     $value = $validation->getValue($attribute);
     if ($allowEmpty && (is_scalar($value) && (string) $value === '' || is_null($value))) {
         return true;
     }
     $origField = $this->getOption('origField');
     $origValue = $validation->getValue($origField);
     if (is_string($value) && $value == $origValue) {
         return true;
     }
     $message = $this->hasOption('message') ? $this->getOption('message') : 'Value not confirmed';
     $validation->appendMessage(new Validation\Message($message, $attribute, 'ConfirmationOfValidator'));
     return false;
 }
Example #17
0
 /**
  * @see \Phalcon\Validation\Validator::validate
  * @param Validation $validator
  * @param $attribute
  * @return bool
  */
 public function validate(Validation $validator, $attribute)
 {
     $value = $validator->getValue($attribute);
     $result = call_user_func($this->_options['callback'], $value);
     if (!$result) {
         $message = isset($this->_options['message']) ? $this->_options['message'] : 'validation failed';
         $validator->appendMessage(new Message($message, $attribute, 'callback'));
     }
     return $result ? true : false;
 }
Example #18
0
 public function validate(\Phalcon\Validation $validation, $field)
 {
     $value = $validation->getValue($field);
     $result = $this->_csrf->verify($value, 'form');
     if (!$result['is_valid']) {
         $validation->appendMessage(new Message($result['message'], $field, 'Csrf'));
         return false;
     }
     return true;
 }
 public function validate(\Phalcon\Validation $validator, $attribute)
 {
     $value = $validator->getValue($attribute);
     if ((bool) getimagesize($value['tmp_name'])) {
         return true;
     } else {
         $validator->appendMessage(new Message('Файл не является изображением. Для загрузки разрешены только изображения.', $attribute, 'upload_image'));
         return false;
     }
 }
Example #20
0
 /**
  * Executes Integer validation
  *
  * @param \Phalcon\Validation $validator
  * @param string $attribute
  * @return boolean
  */
 public function validate(\Phalcon\Validation $validator, $attribute)
 {
     $value = $validator->getValue($attribute);
     $msg = $this->getOption('message');
     if (ctype_digit(strval($value)) == false) {
         $validator->appendMessage(new Message($msg, $attribute, 'IntegerValidator'));
         return false;
     }
     return true;
 }
Example #21
0
 /**
  * Executes network / ip validation, accepts the following parameters as attributes:
  *      version     : ipv4, ipv6, all (default)
  *      noReserved  : true, false (default)
  *      noPrivate   : true, false (default)
  *      noSubnet    : true, false (default)
  *
  *
  * @param \Phalcon\Validation $validator
  * @param string $attribute
  * @return boolean
  */
 public function validate(\Phalcon\Validation $validator, $attribute)
 {
     $result = true;
     $value = $validator->getValue($attribute);
     $msg = $this->getOption('message');
     // parse filter options
     $filterOpt = 0;
     switch (strtolower($this->getOption('version'))) {
         case "ipv4":
             $filterOpt |= FILTER_FLAG_IPV4;
             break;
         case "ipv6":
             $filterOpt |= FILTER_FLAG_IPV6;
             break;
         default:
             $filterOpt |= FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6;
     }
     if ($this->getOption('noReserved') === true) {
         $filterOpt |= FILTER_FLAG_NO_RES_RANGE;
     }
     if ($this->getOption('noPrivate') === true) {
         $filterOpt |= FILTER_FLAG_NO_PRIV_RANGE;
     }
     // split network
     if (strpos($value, "/") !== false) {
         $parts = explode("/", $value);
         if (count($parts) > 2 || !ctype_digit($parts[1])) {
             // more parts then expected or second part is not numeric
             $result = false;
         } else {
             $mask = $parts[1];
             $value = $parts[0];
             if (strpos($parts[0], ".")) {
                 // most likely ipv4 address, mask must be between 0..32
                 if ($mask < 0 || $mask > 32) {
                     $result = false;
                 }
             } else {
                 // probably ipv6, mask must be between 0..128
                 if ($mask < 0 || $mask > 128) {
                     $result = false;
                 }
             }
         }
     }
     if (filter_var($value, FILTER_VALIDATE_IP, $filterOpt) === false) {
         $result = false;
     }
     if (!$result) {
         // append validation message
         $validator->appendMessage(new Message($msg, $attribute, 'NetworkValidator'));
     }
     return $result;
 }
 public function validate(\Phalcon\Validation $validator, $attribute)
 {
     $value = $validator->getValue($attribute);
     $allowed = $this->getOption('allowed');
     $ext = strtolower(pathinfo($value['name'], PATHINFO_EXTENSION));
     if (in_array($ext, $allowed)) {
         return true;
     }
     $validator->appendMessage(new Message('Запрещённый формат файла. Разрешённые форматы: ' . implode(', ', $allowed) . '.', $attribute, 'upload_type'));
     return false;
 }
Example #23
0
 public function validate(\Phalcon\Validation $validator, $attribute)
 {
     $this->validator = $validator;
     $this->attribute = $attribute;
     $this->messages = array();
     $value = $validator->getValue($attribute);
     if (is_array($value) && $this->getOption('cloneable') instanceof \Vegas\Forms\Element\Cloneable) {
         return $this->validateAll($value);
     }
     $this->appendSingleMessage('Validator suitable only for Cloneable element.');
     return false;
 }
Example #24
0
 public function validate(\Phalcon\Validation $validation, $field)
 {
     $response = $validation->getValue($field);
     $answer = \Baseapp\Library\Recaptcha::check(\Phalcon\DI::getDefault()->getShared('request')->getClientAddress(), $response);
     if ($answer['error'] == TRUE) {
         // Captcha is incorrect
         $recap_error = $answer['error'];
         $validation->appendMessage(new \Phalcon\Validation\Message($recap_error, $field, "Recaptcha"));
         return false;
     }
     return TRUE;
 }
Example #25
0
 /**
  * Executes MinMax validation
  *
  * @param \Phalcon\Validation $validator
  * @param string $attribute
  * @return boolean
  */
 public function validate(\Phalcon\Validation $validator, $attribute)
 {
     $value = $validator->getValue($attribute);
     $min = $this->getOption('min');
     $max = $this->getOption('max');
     $msg = $this->getOption('message');
     if (is_numeric($value) == false || $value < $min || $value > $max) {
         $validator->appendMessage(new Message($msg, $attribute, 'MaxMinValidator'));
         return false;
     }
     return true;
 }
Example #26
0
 public function getValue($attribute)
 {
     if (null !== ($ret = parent::getValue($attribute))) {
         return $ret;
     } else {
         if (!empty($this->_filters[$attribute])) {
             return $this->filter->sanitize(null, $this->_filters[$attribute]);
         } else {
             return null;
         }
     }
 }
 /**
  * Executes the validation
  *
  * @param mixed $validation
  * @param string $attribute
  * @return bool
  */
 public function validate(Validation $validation, $attribute)
 {
     $includeCountryCode = $this->getOption('must_include_country_code', false);
     $includePlus = $this->getOption('must_include_plus', false);
     $pattern = '/^' . '[+]' . ($includePlus ? '' : '?') . ($includeCountryCode ? '234[0]?' : '((234[0]?)|0)') . '[\\d]{10}$/';
     $this->setOption('pattern', $pattern);
     if (!preg_match($pattern, $validation->getValue($attribute))) {
         $this->addMessageToValidation($validation, 'Invalid :field supplied', $attribute, 'NigerianPhoneNumber');
         return false;
     } else {
         return true;
     }
 }
Example #28
0
 /**
  * Executes the validation
  *
  * @param Phalcon\Validation $validator
  * @param string $attribute
  * @return boolean
  */
 public function validate(\Phalcon\Validation $validator, $attribute)
 {
     $value = $validator->getValue($attribute);
     if (!filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
         $message = $this->getOption('message');
         if (!$message) {
             $message = 'The IP is not valid';
         }
         $validator->appendMessage(new Message($message, $attribute, 'Ip'));
         return false;
     }
     return true;
 }
Example #29
0
 public function validate(Validation $validation, $attribute)
 {
     $value = $validation->getValue($attribute);
     if (!preg_match('/1[3578][0-9]{9}/', $value)) {
         $message = $this->getOption('message');
         if (!$message) {
             $message = '请输入正确的手机号';
         }
         $validation->appendMessage(new Message($message, $attribute, 'Mobile'));
         return false;
     }
     return true;
 }
Example #30
0
 /**
  * Executes validation
  *
  * @param \Phalcon\Validation $validator
  * @param string $attribute
  * @return boolean
  */
 public function validate(\Phalcon\Validation $validator, $attribute)
 {
     $value = $validator->getValue($attribute);
     $domain = $this->getOption('domain');
     $msg = $this->getOption('message');
     foreach (explode(",", $value) as $valItem) {
         if (!in_array($valItem, $domain)) {
             $validator->appendMessage(new Message($msg, $attribute, 'CsvListValidator'));
             return false;
         }
     }
     return true;
 }