cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Phalcon\Validation\Validator\Alpha work have aquestion

Open pz9042 opened this issue 7 years ago • 6 comments

$validator = new \Phalcon\Validation();
    $validator->add(
        "username",
        new \Phalcon\Validation\Validator\Alpha(
            [
                "message" => ":field must contain only letters",
                'allowEmpty' =>false,
            ]
    )
);

 $data = ['username' =>'1'];
$message = $validator ->validate( $data );

it will output username must contain only letters ,but if $data have no the username index or the value is empty. the $message will return emty array. the correct validate result should't be fail?

pz9042 avatar Jun 27 '19 07:06 pz9042

You need to use Phalcon\Validation\Validator\PresenceOf for the field to be required

ekmst avatar Jun 27 '19 07:06 ekmst

You need to use Phalcon\Validation\Validator\PresenceOf for the field to be required

I have set allowEmpty be false, if use\Phalcon\Validation\Validator\Alnum,it will work。

pz9042 avatar Jun 27 '19 08:06 pz9042

What version of Phalcon are you using?

ekmst avatar Jun 27 '19 08:06 ekmst

3.4.3

pz9042 avatar Jun 27 '19 08:06 pz9042

allowEmpty by default false (https://github.com/phalcon/cphalcon/blob/3.4.x/phalcon/validation.zep#L592) and should be used for setting to true or array

Alpha validation use preg_match preg_match("/[^[:alpha:]]/imu", value), try set value as empty string or null. (https://github.com/phalcon/cphalcon/blob/3.4.x/phalcon/validation/validator/alpha.zep#L73)

Different validation types can have different behavior if data key is empty string or null.

So, for correct validation "not empty", use PresenceOf , as @ekmst said.

olegatro avatar Aug 19 '19 14:08 olegatro

We only accept bug reports, new feature requests and pull requests in GitHub. For questions regarding the usage of the framework, support requests, and questions like this please visit the official forums.

sergeyklay avatar Aug 19 '19 21:08 sergeyklay