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