We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

InclusionValidator

Can somebody tell me whats wrong with my code ? I followed the REST api tutorial and applied what they said but I'm still having the same error " Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Mvc\Model\Validator\Inclusionin given in" . When I implement the interface it still doesn't work.

public function validation()
{

    $this->validate(
        new InclusionInValidator (
            array(
                "field" => 'gender',
                "domain" => array(
                    'M',
                    'F'
                )
            )
        )
    );

    $this->validate(
        new Uniqueness(
            array(
                "field" => "user_name",
                "message" => "This username is already used, please choose another one"
            )
        )
    );

    if ($this->validationHasFailed() == true) {
        return false;
    }
}

}

It looks like you are using 2.1.x there is new api for validation for 2.1.x. Check it here - https://blog.phalcon.io/post/phalcon-2-1-beta-released, also from the latest 2.1.x you don't need to use 'model' => $this, in Uniqueness validator

Thanks for the answer but the link you've sent is dead and I don't relly get what you mean by I don't need to user "model' => $this,inUniqueness` validator"

It looks like you are using 2.1.x there is new api for validation for 2.1.x. Check it here - https://blog.phalcon.io/post/phalcon-2-1-beta-released, also from the latest 2.1.x you don't need to use 'model' => $this, in Uniqueness validator



145.0k
Accepted
answer

Thanks a lot , but the InclusionIn is still now working, can you help me please with it ?

Beacause it's with ,, here you go - https://blog.phalcon.io/post/phalcon-2-1-beta-released

But what's not working ? You jsut need to change your code to:

public function validation()
{
    $validation = new Phalcon\Validation();

    $validation->add('gender',
        new Phalcon\Validation\Validator\InclusionIn (
            array(
                "domain" => array(
                    'M',
                    'F'
                )
            )
        )
    );

    $validation->add(
        new Phalcon\Validation\Validator\Uniqueness('user_name',
            array(
                "message" => "This username is already used, please choose another one"
            )
        )
    );

    return $this->validate($validation);
}