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

How to execute own validator in model ??

I have model in models/Posts.php

<?php

use Phalcon\Mvc\Model\Validator; use Phalcon\Mvc\Model\Validator\PresenceOf; use Phalcon\Mvc\Model\Validator\StringLength; use Phalcon\Mvc\Model\Validator\Uniqueness; use Phalcon\Mvc\Model\Validator\OtherThanActive;

class Posts extends \Phalcon\Mvc\Model {

public function initialize()
{
    $this->belongsTo('id_user', 'Users', 'id');
}

public function validation()
{
    $this->validate(new PresenceOf([
        'field' => 'title',
        'message' => 'Tytuł nie może być pusty'
    ]));

    $this->validate(new StringLength([
        'field' => 'title',
        'min' => 5,
        'max' => 20,
        'message' => 'Długość musi być między 5 a 20 !'
    ]));

    $this->validate(new Uniqueness([
        'field' => 'title',
        'message' => 'Tytuł musi być unikalny. Aktualny tytuł istnieje'
    ]));

    $this->validation(new OtherThanActive([
        'field' => 'title'
    ]));

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

}

And I created validator in /app/validators/PostsValidators.php (I added in config and loader this dir)

Code of validator:

use Phalcon\Mvc\Model\Validator; use Phalcon\Mvc\Model\ValidatorInterface; use Phalcon\Validation;

class OtherThanActive extends Validator implements ValidatorInterface {

public function validate(Validation $validator, $attr)
{
    $postId = $this->getOption('postId');
    $field = $this->getOption('field');

    $active = \Posts::findFirst($attr['postId'])->users->active; 
    if($attr['postTitle'] == $active)
    {
        $validator->appendMessage(new Message('Title can not be the same as user active status -- just test. stupid message'));
        return false;
    }

    return true;
}

}

When I want use my walidator in model I have error that OtherThanActive class not found. How in phalcon we can include own validators ???

Thanks



4.0k

Try to change use Phalcon\Mvc\Model\Validator\OtherThanActive; to use \OtherThanActive; in Posts model.



2.1k
edited Jul '15

unfortunatelly ... does not work ..

Fatal error: Class 'OtherThanActive' not found in C:\xampp\htdocs\phJoins\app\models\Posts.php on line 39

any other ideas ? This is basic feature in all applications :)



4.0k
edited Jul '15

0) Which version of Phalcon are you using?

1) Rename /app/validators/PostsValidators.php to /app/validators/OtherThanActive.php

2) I think, you created wrong validator OtherThanActive validate function.

<?php

use Phalcon\Mvc\Model\Message;
use Phalcon\Mvc\Model\Validator;
use Phalcon\Mvc\Model\ValidatorInterface;

class OtherThanActive extends Validator implements ValidatorInterface
{

    /**
     * @param \Phalcon\Mvc\EntityInterface $record
     *
     * @return bool
     */
    public function validate(\Phalcon\Mvc\EntityInterface $record)
    {
       //your validation logic
    }
}

i tried this simple example and it's working in my case



2.1k
edited Jul '15

Where did you create validator ? can you give me your path ? Did you have to add some configuration in other files ?

I'm trying use in bootstrap:

$loader->registerNamespaces([
    'MyValidator\Validator' => '../app/validators/' 
]);

but this is probably wrong.. It shoud works immediately... Maybe your instruction give me solution.

Your code returns:

Fatal error: Declaration of PostsOtherThanActive::validate() must be compatible with Phalcon\Mvc\Model\ValidatorInterface::validate(Phalcon\Mvc\ModelInterface $record) in

Thanks



4.0k

Which version of Phalcon are you using?



2.1k
edited Jul '15
  • Author Phalcon Team and contributors

  • Version 2.0.3

  • Build Date Jun 9 2015 19:50:13

  • Powered by Zephir Version 0.6.3a



4.0k
edited Jul '15

Ok. Let's try again :)

1) Your validator class name must be the same as file name.

2) change PostsOtherThanActive validate function parameter type

<?php

use Phalcon\Mvc\Model\Message;
use Phalcon\Mvc\Model\Validator;
use Phalcon\Mvc\Model\ValidatorInterface;

class PostsOtherThanActive extends Validator implements ValidatorInterface
{

    /**
     * @param \Phalcon\Mvc\EntityInterface $record
     *
     * @return bool
     */
    public function validate(\Phalcon\Mvc\ModelInterface $record)
    {
       //your validation logic
    }
}

3) In bootstrap add:

$loader->registerDirs(array('../app/validators/'));

remove:

$loader->registerNamespaces([
    'MyValidator\Validator' => '../app/validators/' 
]);


2.1k

I have your version of code but without

public function validate(\Phalcon\Mvc\ModelInterface $record)

My phalcon need to have

public function validate(Validation $validator = null, $attr = null)

Now PHP is blocking when I try execute my validator... Looks like Bug in Phalcon.. ( other controllers works fine )

What version do you have ? Maybe I need change phalcon.DLL (windows 8)



4.0k
edited Jul '15

I'm using latest version of Phalcon 2.0.4. Try update https://phalcon.io/en/download/windows



2.1k

The reaon was Phalcon version. ValidatorInterface is different in 2.0.3 and 2.0.4.

In version 2.0.4 your code works fine.

Thanks a lot.