/** * {@inheritdoc} * * @param \Phalcon\Mvc\EntityInterface $record * @return boolean * @throws \Phalcon\Mvc\Model\Exception */ public function validate(EntityInterface $record) { $field = $this->getOption('field'); if (false === is_string($field)) { throw new Exception('Field name must be a string'); } $value = $record->readAttribute($field); if (true === $this->isSetOption('allowEmpty') && empty($value)) { return true; } if (false === $this->isSetOption('places')) { throw new Exception('A number of decimal places must be set'); } if ($this->isSetOption('digits')) { // Specific number of digits $digits = '{' . (int) $this->getOption('digits') . '}'; } else { // Any number of digits $digits = '+'; } if ($this->isSetOption('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) { // Check if the developer has defined a custom message $message = $this->getOption('message') ?: sprintf('%s must contain valid decimal value', $field); $this->appendMessage($message, $field, 'Decimal'); return false; } return true; }
/** * @param \Phalcon\Mvc\EntityInterface $record * * @return boolean * * @throws \Phalcon\Validation\Exception */ public function validate(\Phalcon\Mvc\EntityInterface $record) { $field = $this->getOption("field"); if (!is_string($field)) { throw new \Phalcon\Validation\Exception("Field name must be a string"); } $value = $record->readAttribute($field); if ($this->isSetOption("allowEmpty") && empty($value)) { return true; } if (!preg_match('/^\\{?[A-Z0-9]{8}-[A-Z0-9]{4}-[1-5][A-Z0-9]{3}-[A-Z0-9]{4}-[A-Z0-9]{12}\\}?$/i', $value)) { $message = $this->getOption("message"); if (empty($message)) { $message = "Field :field must be a valid UUID"; } $replacePairs = [":field" => $field]; $this->appendMessage(strtr($message, $replacePairs), $field, "Uuid"); return false; } $allowedVersions = $this->getOption("allowedVersions"); if (empty($allowedVersions)) { $allowedVersions = [1, 2, 3, 4, 5]; } if (!in_array(substr($value, 14, 1), $allowedVersions)) { $message = $this->getOption("messageVersion"); if (empty($message)) { $message = "Field :field must be one of the following UUID versions: :versions"; } $replacePairs = [":field" => $field, ":versions" => implode(", ", $allowedVersions)]; $this->appendMessage(strtr($message, $replacePairs), $field, "Uuid"); return false; } return true; }
/** * Validates that the record is unique * * @param $record * @return boolean */ public function validate(\Phalcon\Mvc\EntityInterface $record) { $field = $this->getOption('field'); if ($record->count(['conditions' => [$field => $record->readAttribute($field)]])) { $this->appendMessage("The " . $field . " must be unique", $field, "Unique"); return false; } return true; }
/** * {@inheritdoc} * * <strong>NOTE:</strong> * for Phalcon < 2.0.4 replace * <code>\Phalcon\Mvc\EntityInterface</code> * by * <code>\Phalcon\Mvc\ModelInterface</code> * * @param EntityInterface $record * * @return bool * @throws Exception */ public function validate(EntityInterface $record) { $field = $this->getOption('field'); if (false === is_string($field)) { throw new Exception('Field name must be a string'); } $fieldValue = $record->readAttribute($field); $value = preg_replace('/[^\\d]/', '', $fieldValue); if ($this->isSetOption('type')) { $type = $this->getOption('type'); switch ($type) { case CardNumber::AMERICAN_EXPRESS: $issuer = substr($value, 0, 2); $result = true === in_array($issuer, [34, 37]); break; case CardNumber::MASTERCARD: $issuer = substr($value, 0, 2); $result = true === in_array($issuer, [51, 52, 53, 54, 55]); break; case CardNumber::VISA: $issuer = $value[0]; $result = $issuer == 4; break; default: throw new Exception('Incorrect type specifier'); } if (false === $result) { $message = $this->getOption('message') ?: 'Credit card number is invalid'; $this->appendMessage($message, $field, "CardNumber"); return false; } } $value = strrev($value); $checkSum = 0; for ($i = 0; $i < strlen($value); $i++) { if ($i % 2 == 0) { $temp = $value[$i]; } else { $temp = $value[$i] * 2; if ($temp > 9) { $temp -= 9; } } $checkSum += $temp; } if ($checkSum % 10 != 0) { $message = $this->getOption('message') ?: 'Credit card number is invalid'; $this->appendMessage($message, $field, "CardNumber"); return false; } return true; }
/** * Executes the validator * * @param mixed $record * @return bool */ public function validate(\Phalcon\Mvc\EntityInterface $record) { //var_dump($record);die(); $field = $this->getOption('field'); $row = $record->count(['conditions' => $field . ' = ?1 AND isdel = 0', 'bind' => [1 => $record->readAttribute($field)]]); if ($row) { $message = $this->getOption('message'); if (!$message) { $message = "Já existe " . $field . " cadastrado"; } $this->appendMessage($message, $field, "Unique"); } return false; }
/** * Validates a method * * @param EntityInterface $record * * @return bool */ public function validate(EntityInterface $record) { $field = $this->getOption('field'); $value = $record->readAttribute($field); if ($value == '') { return true; } else { if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { $this->appendMessage($field . ' (Invalid Email): ' . $value, $field); return false; } else { return true; } } }
/** * {@inheritdoc} * * @param \Phalcon\Mvc\EntityInterface $record * @return boolean */ public function validate(EntityInterface $record) { $field = $this->getOption('field'); $fieldConfirmation = $this->getOption('field_confirmation'); $fieldValue = $record->readAttribute($field); $fieldConfirmationValue = $record->readAttribute($fieldConfirmation); $message = $this->getOption('message') ? $this->getOption('message') : 'Both fields should contain equal values'; if ($fieldConfirmationValue) { if ($fieldValue !== $fieldConfirmationValue) { $this->appendMessage($message, $fieldConfirmation, 'ConfirmationOf'); return false; } } return true; }
/** * @param \Phalcon\Mvc\EntityInterface $record * * @return boolean * * @throws \Phalcon\Validation\Exception */ public function validate(\Phalcon\Mvc\EntityInterface $record) { $field = $this->getOption("field"); if (!is_string($field)) { throw new \Phalcon\Validation\Exception("Field name must be a string"); } $value = $record->readAttribute($field); if ($this->isSetOption("allowEmpty") && empty($value)) { return true; } if (!preg_match('/^[a-zA-Z0-9\\-]+$/i', $value)) { $message = $this->getOption("message"); if (empty($message)) { $message = ":field does not have a valid slug format"; } $replacePairs = [":field" => $field]; $this->appendMessage(strtr($message, $replacePairs), $field, "Slug"); return false; } return true; }
/** * @param \Phalcon\Mvc\EntityInterface $record * * @return boolean * * @throws \Phalcon\Validation\Exception */ public function validate(\Phalcon\Mvc\EntityInterface $record) { $field = $this->getOption("field"); if (!is_string($field)) { throw new \Phalcon\Validation\Exception("Field name must be a string"); } $value = $record->readAttribute($field); if ($this->isSetOption("allowEmpty") && empty($value)) { return true; } if (!preg_match('/^[A-Z]{2}$/i', $value)) { $message = $this->getOption("message"); if (empty($message)) { $message = "Field :field must be a valid ISO 4217 currency code"; } $replacePairs = [":field" => $field]; $this->appendMessage(strtr($message, $replacePairs), $field, "Iso4217CurrencyCode"); return false; } return true; }
/** * @param \Phalcon\Mvc\EntityInterface $record * * @return boolean * * @throws \Phalcon\Validation\Exception */ public function validate(\Phalcon\Mvc\EntityInterface $record) { $field = $this->getOption("field"); if (!is_string($field)) { throw new \Phalcon\Validation\Exception("Field name must be a string"); } $value = $record->readAttribute($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 have a valid hostname"; } $replacePairs = [":field" => $field]; $this->appendMessage(strtr($message, $replacePairs), $field, "Hostname"); return false; } return true; }
/** * {@inheritdoc} * * <strong>NOTE:</strong> * for Phalcon < 2.0.4 replace * <code>\Phalcon\Mvc\EntityInterface</code> * by * <code>\Phalcon\Mvc\ModelInterface</code> * * @param EntityInterface $record * * @return boolean * @throws Exception */ public function validate(EntityInterface $record) { $field = $this->getOption('field'); if (false === is_string($field)) { throw new Exception('Field name must be a string'); } $value = $record->readAttribute($field); if (true === $this->isSetOption('allowEmpty') && empty($value)) { return true; } if (false === $this->isSetOption('min') || false === $this->isSetOption('max')) { throw new Exception('A minimum and maximum must be set'); } $maximum = $this->getOption('max'); $minimum = $this->getOption('min'); if ($value < $minimum || $value > $maximum) { // Check if the developer has defined a custom message $message = $this->getOption('message') ?: sprintf('%s is not between a valid range', $field); $this->appendMessage($message, $field, 'Between'); return false; } return true; }