Archived
1
0
Fork 0
This repository has been archived on 2026-04-03. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
httpcb/app/library/Validation/Validator/Alpha.php

64 lines
1.6 KiB
PHP

<?php
namespace Httpcb\Validation\Validator;
use Phalcon\Validation\Message;
use Phalcon\Validation\Validator as BaseValidator;
/**
* The same as the default Alpha validator shipped with phalcon.
* But this validator supports the option "allowSpace" that also
* allow whitespaces.
*
* @package Validation\Validator
*/
class Alpha extends BaseValidator
{
/**
* Executes the validation
*
* @param mixed $validation
* @param string $attribute
* @return bool
*/
public function validate(\Phalcon\Validation $validation, $attribute)
{
$allowSpace = $this->getOption('allowSpace', false);
$charlist = '[:alpha:]';
if ($allowSpace) {
$charlist .= '[:space:]';
}
$value = $validation->getValue($attribute);
if (preg_match("/[^{$charlist}]/imu", $value)) {
$label = $this->getOption('label');
if (empty($label)) {
$label = $validation->getLabel($attribute);
}
$message = $this->getOption('message');
if (empty($message)) {
$message = $validation->getDefaultMessage('Alpha');
}
$replace = [ ":field" => $label ];
$code = $this->getOption("code");
if (is_array($code)) {
$code = $code[$attribute];
}
$message = str_replace(array_keys($replace), $replace, $message);
$msg = new Message($message, $attribute, "Alpha", $code);
$validation->appendMessage($msg);
return false;
}
return true;
}
}