使用Phalcon模型进行自定义验证

时间:2015-07-14 02:19:12

标签: php validation phalcon

我正在尝试为我的模型实现自定义验证。我目前正在成功使用Phalcon库中包含的验证器,但是当我尝试实现我的时,它不起作用。这是我的代码:

    <?php

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

class MaxMinValidator extends Validator implements ValidatorInterface
{

    public function validate(EntityInterface $model)
    {
        $field  = $this->getOption('field');

        $min    = $this->getOption('min');
        $max    = $this->getOption('max');

        $value  = $model->$field;

        if ($min <= $value && $value <= $max) {
            $this->appendMessage(
                "The field doesn't have the right range of values",
                $field,
                "MaxMinValidator"
            );
            return false;
        }
        return true;
    }
}

我正试图在我的模型中使用它:

public function validation()
{
        $this->validate(new MaxMinValidator(
            array(
                "field" => "Email",
                "min"   => 10,
                "max"   => 100
            )
        ));
 }

正确加载了类,但是没有执行验证功能,并且在尝试验证后我的代码停止执行。 注意:这与找到here的代码完全相同。

2 个答案:

答案 0 :(得分:2)

1.检查您的验证器类是否正确加载。 (创建一个函数并写死(“sth”);)如果调用该函数导致你的应用程序死亡,你就正确地加载了这个类。

2.检查您的phalcon版本是否在2.0.5以下,您应该将验证码修改为:

<?php

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

class MaxMinValidator extends Validator implements ValidatorInterface
{
    public function validate(\Phalcon\Mvc\ModelInterface $model)
    {
         $field  = $this->getOption('field');
         $min    = $this->getOption('min');
         $max    = $this->getOption('max');

         $value  = $model->$field;

         if ($min <= $value && $value <= $max) {
             $this->appendMessage(
             "The field doesn't have the right range of values",
             $field,
             "MaxMinValidator"
             );
            return false;
        }
        return true;
    }
}

如果您的phalcon版本是2.0.5并且您的类已加载但是您无法看到验证错误消息,请将此方法添加到您的模型类中:

public function getMessages() {
    $messages = array();
    foreach (parent::getMessages() as $message) {
        switch ($message->getType()) {
            case 'InvalidCreateAttempt':
                $messages[] = 'The record cannot be created because it already exists';
                break;
            case 'InvalidUpdateAttempt':
                $messages[] = 'The record cannot be updated because it already exists';
                break;
            case 'PresenceOf':
                $messages[] = 'The field ' . $message->getField() . ' is mandatory';
                break;
            case 'MaxMinValidator':
                $messages[] = 'The field ' . $message->getField() . ' is not in range';
                break;
        }
    }
    return $messages;
}

答案 1 :(得分:0)

你检查了整个功能吗?

validationHasFailed

我认为您错过了运行filter { multiline{ pattern => "^%{TIMESTAMP_ISO8601}" what => "previous" negate=> true } # Delete trailing whitespaces mutate { strip => "message" } # Delete \n from messages mutate { gsub => ['message', "\n", " "] } # Delete \r from messages mutate { gsub => ['message', "\r", " "] } grok { match => { "message" => "%{TIMESTAMP_ISO8601:time} \[%{NUMBER:thread}\] %{LOGLEVEL:loglevel} %{JAVACLASS:class} - %{GREEDYDATA:msg}" } } if "Exception" in [msg] { mutate { add_field => { "msg_error" => "%{msg}" } } } } 方法。