Spaces removing in Validation values?

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?

The Buildin Regex validator works little different look here

Basically you have to get in matches result on index 0 equals or not at the input value

Good luck



312
edited Apr '18

I think you didn't understand my problem. My main problem is that $validator->getValue('name_of_field'); returns string without spaces. For example setting value without form and validation I can got that value with all spaces:

$user = new User;
$user->save([
    'username' => 'Mr Maxie'
]);
// ...
User::findFirst(1)->username; // 'Mr Maxie'

But with simplest form with simplest validations (without any filters) I got:

<input type='text' name='username' value='Mr Maxie'>
//...
User::findFirst(1)->username; // 'MrMaxie'

I found out spaces are removed by $validation->getValue('name_of_field') (that how I found out this, are described in my first post).

(Gist with code cut from my project)

I hear that Phalcon have small differences between versions for other OSs, so I use: Windows, PHP 5.6.35, Phalcon 3.3.2

I don't understand something? I forget about something?