Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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);
    }

}


64.3k

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



919
Accepted
answer
edited Aug '16

Thanks, beforeValidation works just as well for me.

edited Aug '16

Also

 'model' => $this,

is not needed.