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 during save

Using phalcon 3.0.0 on windows wamp.

I want to validate the presence of the distance property in my model.

distance is also set as not NULL in mysql.

When I run $trip->save() I get an erorr message distance is required and my validator isn't called. Is there any way to make the validator run before other database checks?

class Trips extends Model {

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

        $validator->add('distance', new PresenceOf([
            'model' => $this,
            'message' => 'Fill in the distance',
        ]));

        dump($this->distance);
        dump($this->validate($validator));
        exit();

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

}


85.5k

I ran into the same problem last night, I opened an issue here https://github.com/phalcon/cphalcon/issues/12180 , but lets see what other will say wether its a bug or feature :D



2.7k
Accepted
answer
edited Aug '16

Thanks, beforeValidation works just as well for me.

edited Aug '16

Also

 'model' => $this,

is not needed.