Archived
1
0
Fork 0

adding app/library/Validation/Validator/Alpha.php

This commit is contained in:
Henrik Hautakoski 2018-03-31 23:11:35 +02:00
parent 3bcb1b4b01
commit d7d10057ae

View 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;
}
}