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 function not working

I have this model

use Phalcon\Mvc\Model,
    Phalcon\Mvc\Model\Validator\PresenceOf;

class Posts extends Model{

    public $id;

    public $title;

    public $content;

    public $createDate;

    public $lastEditDate;

    public $status;

    public $slug;

    public $author_id;

    public function initialize(){

        $this->belongsTo('author_id', '\App\Models\Users', 'id', array(
            'alias' => 'author'
        ));

        $this->hasMany('id', '\App\Models\PostsComments', 'post_id', array(
            'alias' => 'comments'
        ));

    }

    public function validation(){

        //title
        $this->validate(new PresenceOf(array(
            'field' =>  'title',
            'message' => 'Un article doit avoir un titre.'
        )));

        $this->validate(new Model\Validator\StringLength(array(
            'field' => 'title',
            'min' => 3,
            'max' => 255,
            'messageMinimum' => "Le titre de l'article est trés cour, il faut au moins 3 caractéres.",
            'messageMaximum' => "Le titre de l'article est trés long, pas plus que 255."
        )));

        //content
        $this->validate(new Model\Validator\PresenceOf(array(
            'field' => 'content',
            'message' => 'Un article sans contenu ?!.',
        )));

        return !$this->validationHasFailed();

    }

}

The Validation function looks ok but when I try to add a post with wrong information, the errors messages still the default Phalcon messages, and whene I add a title with 2 chars length (lower than the min of StringLength), no error detected...

Hi @sn0opr,

and if you try this:

<?php

use Phalcon\Mvc\Model,
    Phalcon\Mvc\Model\Validator;

class Posts extends Model{

    public $id;

    public $title;

    public $content;

    public $createDate;

    public $lastEditDate;

    public $status;

    public $slug;

    public $author_id;

    public function initialize(){

    $this->belongsTo('author_id', '\App\Models\Users', 'id', array(
    'alias' => 'author'
    ));

    $this->hasMany('id', '\App\Models\PostsComments', 'post_id', array(
    'alias' => 'comments'
    ));

    }

    public function validation()
    {

        //title
        $this->validate(new Validator\PresenceOf(array(
        'field' =>  'title',
        'message' => 'Un article doit avoir un titre.'
        )));

        $this->validate(new Validator\StringLength(array(
        'field' => 'title',
        'min' => 3,
        'max' => 255,
        'messageMinimum' => "Le titre de l'article est trés cour, il faut au moins 3 caractéres.",
        'messageMaximum' => "Le titre de l'article est trés long, pas plus que 255."
        )));

        //content
        $this->validate(new Validator\PresenceOf(array(
        'field' => 'content',
        'message' => 'Un article sans contenu ?!.',
        )));

        return !$this->validationHasFailed();
    }

}

When I used an explicit function like use Phalcon\Mvc\Model\Validator\PresenceOf; and later on i tried to use:

    $this->validate(new Model\Validator\Uniqueness([
            'field' => 'email',
            'message' => 'Your email is already in use'
        ]));

in my code, I almost always ended up with errors.



22.8k

Hi @screenas Thank you for trying to help, but it still not working :(

Could you post some code of your controller? I wrote a sample App with your Model and the Validation worked.



26.3k

You need to swiitch off the default Phalcon's notNullValidators. Check below post out. And the posts it links to as well.

https://forum.phalcon.io/discussion/2560/orm-validation-before-my-own-validations