Hello, I found something that looks like a little bug. I created class for validating my form and I tried to check if username contains multiple whitespaces between other characters using Regex validation.
use Phalcon\Validation\Validator\Regex;
//...
$username = new Text('username');
$username->addValidators(
new Regex([
'pattern' => '/^[^ ]*?([ ][^ ]+)?$/i',
'message' => 'Invalid username - too many spaces',
])
);
$this->add($username);
//...
I can't had reach that error. But when I was tried to search for the same way other characters (for example -
) and tested all that regexs (including that with spaces, which was I showed in example code above) using preg_match()
and everything was ok.
Some tests later I tried to write validator by myself and... I had same problem. I found that Validation::getValue()
returns everytime value without spaces. I used little "trick" for this problem:
use \Phalcon\Validation;
use \Phalcon\Validation\Validator;
use \Phalcon\Validation\Message;
class RegexValidator extends Validator {
function validate(Validation $validator, $attribute) {
// $validator->getValue($attribute); returns string without spaces
$data = $validator->getData();
if(!array_key_exists($attribute, $data))
$data[$attribute] = '';
$value = $data[$attribute];
$pattern = $this->getOption('pattern');
if(preg_match($pattern, $value) > 0) {
return true;
}
$message = $this->getOption('message');
if(!$message) {
$message = '--REGEX-ERROR--';
}
$validator->appendMessage(
new Message($message, $attribute)
);
return false;
}
}
But, it's bug?