【问题标题】:Own validator in Phalcon.. How to use created own validatorPhalcon 中自己的验证器.. 如何使用创建的自己的验证器
【发布时间】:2015-09-28 07:06:29
【问题描述】:

如何使用我创建的验证器?

我在 models/Posts.php 中有模型

<?php
class Posts extends \Phalcon\Mvc\Model
{ 
     public function validation()
     {
         $this->validation(new OtherThanActive([
             'field' => 'title'
         ]));

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

我在 /app/validators/PostsValidators.php 中创建了验证器 (我在配置中添加并加载此目录)

验证器代码:

<?php
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('Here is error message...'));
            return false;
        }

        return true;
    }
}
?>

当我想在模型中使用我的 walidator 时,我遇到了找不到 OtherThanActive 类的错误。

我们如何在 phalcon 中包含自己的验证器???

谢谢

【问题讨论】:

    标签: php forms validation phalcon


    【解决方案1】:

    我知道你说过你是,但是,我猜你没有加载验证器文件夹。

    使用 Phalcon 加载器执行此操作:

    (new \Phalcon\Loader())
        ->registerDirs(['app/validators/'])
        ->register();
    

    【讨论】:

      猜你喜欢
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 2010-12-24
      相关资源
      最近更新 更多