adding app/library/Validation/Validator/Alpha.php
This commit is contained in:
parent
3bcb1b4b01
commit
d7d10057ae
1 changed files with 66 additions and 0 deletions
66
app/library/Validation/Validator/Alpha.php
Normal file
66
app/library/Validation/Validator/Alpha.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace 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');
|
||||
}
|
||||
|
||||
//var_dump($message);exit;
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
Reference in a new issue