简体   繁体   中英

In CakePHP validation, do I need a notEmpty rule if I have another rule like alphaNumeric and allowEmpty is false?

If I have a validation rule such as

alphaNumeric' => array(
    'rule' => array('alphaNumeric'),
    'allowEmpty' => false),

Is there any need to have a notEmpty rule? As I understand it, the allowEmpty being set to false will consider empty values a violation of the alphaNumeric rule, so other than if I wanted to define two different error messages, is there any need for a notEmpty rule?

(Another way to ask this question: is there some separate functionality that a standalone notEmpty rule would provide or be necessary for, other than to give a separate custom message, that I'm not seeing?)


To be perfectly clear: I understand that idea that notEmpty is a standalone rule, where allowEmpty is an attribute of a rule. That's not my question. My question is, is there any need or value to adding a notEmpty rule (other than the custom message that it would allow you to have for that rule), if you already have an alphaNumeric (or some other similar) rule you can just add allowEmpty = false to? Is there any difference in what the rule vs the attribute does, other than the rule being stand alone?

It really depends on the "other" rule that you're using.

You can see exactly what each rule is ACTUALLY checking for in the CakePHP Validation utility:

https://github.com/cakephp/cakephp/blob/44b7d013ae304a05699179bb4ea0077956c57e10/lib/Cake/Utility/Validation.php

For instance, in that file you can see the alphanumiric check:

public static function alphaNumeric($check) {
    if (is_array($check)) {
        extract(self::_defaults($check));
    }

    if (empty($check) && $check != '0') {
        return false;
    }
    return self::_check($check, '/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]+$/Du');
}

In the case of alphanumeric, you can see that it has an empty check already, so you shouldn't also need the allowEmpty=>false rule.

Lastly, to your point, the only benefit I see in adding it as a separate rule is that you can give a better error message to the user.

Please read... http://book.cakephp.org/2.0/en/models/model-attributes.html

Model attributes allow you to set properties that can override the default model behavior and rules in your context is the business logic of your application.

The answer of your question lies in the below link: http://book.cakephp.org/2.0/en/models/data-validation.html#allowempty

Actually you are absolutely correct, if you there is field in which you have to apply more than one validation, eventually one is nonEmpty in that case you can simply use allowEmpty=>false. But if your datafield requires only one validation for non empty check in that case you should use nonEmpty for better understanding of your code!

I guess I had made my point...thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM