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

Model validation not working

Hi guys,

I am using the below function to validate the uniqueness of 2 form fields in my model. This used to work just fine, but at some point (after 2-3 phalcon upgrades) something changed and it's not working anymore.

public function validation()
{
    $this->validate(
        new Uniqueness(
            array(
                "fields"   => array("email", "vat_no"),
                "message" => "This email of VAT number is already registered"
            )
        )
    );

    return $this->validationHasFailed() != true;
}

And the message I am getting is

Catchable fatal error: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Mvc\Model\Validator\Uniqueness given

As per the docs, I assume I am doing everything correctly... Can anyone shed some light on this?

Thanks!



11.6k

phalcon/model/validation have moved to phalcon/validation in phalcon 3.0

edited Aug '16

phalcon/model/validation have moved to phalcon/validation in phalcon 3.0

So this:

use Phalcon\Mvc\Model\Validator\Uniqueness;

needs to look like what? Becase I read the documentation and my code looks like the example

Also, my form validatiors don't seem to work either... I assume the cause is similar?



1.6k
Accepted
answer
edited Aug '16

Yeah, that's a bit pain in the neck. They have described it here, unfortunately it is not updated in documentation just yet.

What you need to do is;

use Phalcon\Validation;
use Phalcon\Validation\Validator\Uniqueness;

// Your model code goes here....

public function validation()
{
    $validator = new Validation();

    $validator->add(
        ["email", "vat_no"],
        new Uniqueness(
            array(
                "model"   => $this,
                "message" => "This email of VAT number is already registered"
            )
        )
    );

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

Although I'm not sure if ["email", "vat_no"] would actually work. I haven't tried it yet but I assume they would add array support considering old version supports it. If not just use string such as; "email"

I hope this helps.

phalcon/model/validation have moved to phalcon/validation in phalcon 3.0

So this:

use Phalcon\Mvc\Model\Validator\Uniqueness;

needs to look like what? Becase I read the documentation and my code looks like the example

Also, my form validatiors don't seem to work either... I assume the cause is similar?

It will work defintely. Also "model" => $this is not needed.

edited Aug '16

OK, after reading through the blog post I understood how it works on 3.0...

Unforunately, I cannot seem to be able to make the form validation work. Does anyone have an example to share?

edited Aug '16

I have code like below i my model:


    public function validation()
    {
        $validator = new Validation();
        $validator->add(
            ["day_of_week"],
            new InclusionIn(array(
                    "domain" => array('monday', 'thuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'),
                    "field" => 'day_of_week',
                )
            )
        );
        return $this->validate($validator);
    }

This function returns false if validation is not passing, but save() operation goes uniterrupted. What am I missing?