Yii2: SkipOnEmpty has no effect in Ad Hoc validation

Created on 11 Nov 2017  路  5Comments  路  Source: yiisoft/yii2

when using Ad Hoc validation, setting SkipOnEmpty property has no effect.

$email = '';
$validator = new yii\validators\EmailValidator();
$validator->skipOnEmpty = true;
$validator->validate($email, $error);

What is the expected result?

as skipOnEmpty is set and it's true, one expects the validator to simply return true;

What do you get instead?

it returns false and adds a error message to $error variable indicating the given value is not a valid email address.
this behavior is inconsistent with the way validators are working in model classes and causes unexpected results and unnecessary if clauses. should it get fixed or is it intentional?

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.12

bug docs

Most helpful comment

people should use yiivalidatorsRequiredValidator class if they need their input fields to be filled. not sure if people relying on unexpected behaviors should keep us from making things better.

Does empty string is a valid email address? No. If EmailValidator::validate() will start returning true for invalid values, this will be not only confusing, this is simple a bug. I will never call this "making things better".

All 5 comments

this behavior is inconsistent with the way validators are working in model classes

Because model validation simply works differently and most of the settings will not work on validate() call.
And skipOnEmpty does not mean "treat empty results as valid" but "do not run validation on attribute if it is empty".

Changing this will break BC.

Because model validation simply works differently and most of the settings will not work on validate() call.

it's better to support as much same features for both model based and ad hoc validation as possible, as they both share the same class. otherwise it's better to introduce new validator classes specific for ad hoc usages, instead of creating the same object and assuming it would just work differently based on where you use it.
as it's stated on official docs, some validators do not support ad hoc usage at all, but for a situation like the example I gave, it just makes sense to set skipOnEmpty property and expect it to work!

And skipOnEmpty does not mean "treat empty results as valid" but "do not run validation on attribute if it is empty".

you're right, but eventually it causes validator to ignore input thus not adding any errors to $errors array, causing a validate call return true.

Changing this will break BC.

people should use yii\validators\RequiredValidator class if they need their input fields to be filled. not sure if people relying on unexpected behaviors should keep us from making things better.

people should use yiivalidatorsRequiredValidator class if they need their input fields to be filled. not sure if people relying on unexpected behaviors should keep us from making things better.

Does empty string is a valid email address? No. If EmailValidator::validate() will start returning true for invalid values, this will be not only confusing, this is simple a bug. I will never call this "making things better".

this _ ignoring empty values _ is default behavior of validators when attached to models. if for some reason you think that's fine for model based validations but not okay with ad hoc, at least mention it on docs so people don't just use validators expecting same behavior but getting different ones.

I do agree with @rob006.

But this $skipOnEmpty's behavior in ad hoc should be clearly noted in phpdoc.

Was this page helpful?
0 / 5 - 0 ratings