Validator\InclusionIn incorrectly compares variables

Hi. I have some problems with Validator. I receive JSON

{"code": "02"}

and code

$a = ["001", "002", "1111111"];
$v = new Validation();
$v->rules("code", [ new V\InclusionIn([
    "domain" => $a, 
    "allowEmpty" => true  ]) 
    ]);
$messages = $v->validate($json);

So InclusionIn use in_array() - we have problem. Becouse 0002 = 002. But in function in_array() exist third parameter strict. How i can use this parametr in InclusionIn? Or i need write my own validator?

edited Oct '19

What's the context for this validation? If it is just being conducted in validation() in your model, the simplest might be to forgoe using Phalcon's validation and just use vanilla PHP as there's no need for duplication with custom validators.

function validatio(){
    $allowed = ["001", "002", "1111111"];
    if(!in_array($this->code,$allowed,TRUE)){
        $Message = new \Phalcon\Mvc\Model\Message(
            'Invalid code',
            'code',
            ''
        );
        $this->appendMessage($Message);
        return FALSE;
    }
    return TRUE;
}

https://docs.phalcon.io/3.4/en/db-models-validation#validating-data-integrity

Edit I see now this post is 26 days old and you've almost certainly moved on. Sorry for necro-posting. I'll leave my response here though, because I think it's relevant.



1.2k
edited 6d ago

Edit I see now this post is 26 days old and you've almost certainly moved on. Sorry for necro-posting. I'll leave my response here though, because I think it's relevant.

Thanks for your answer. I solved this problem with callback function:

    $v = new Validation();
    $v->rules("code", [ new V\Callback([
        "message" => "Category does not exist",
        "allowEmpty" => true,
        "callback" => function($data) {
            if (!in_array($data->code, \Rules::arrayCatalogs(), true)) {
                return false;
            }

            return true;
        }
    ]) ]);
    $messages = $v->validate($json);