简体   繁体   English

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

[英]Custom Validation with Phalcon Model

I am trying to implement custom validation for my models. 我正在尝试为我的模型实施自定义验证。 I am currently using successfully the included validators from Phalcon library, but when I try to implement mine, it doesn't work. 我目前正在成功使用Phalcon库中包含的验证器,但是当我尝试实现我的方法时,它不起作用。 Here is my code: 这是我的代码:

    <?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;
    }
}

I am trying to use it in my model like this: 我试图像这样在我的模型中使用它:

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

The class is correctly loaded, but doesn't execute the validation function and my code stops being executed after trying to validate. 该类已正确加载,但不执行验证功能,并且在尝试验证后我的代码停止执行。 Note: This is the exact same code as found here . 注意:这是与此处找到的完全相同的代码。

1.Check that your validator class is loaded properly. 1.检查您的验证器类是否正确加载。 (create a function and write die("sth");) if calling that function causes your app to die you loaded the class properly. (创建一个函数并编写die(“ sth”);),如果调用该函数导致您的应用程序死亡,则您正确加载了该类。

2.Check your phalcon version if it under 2.0.5 you should modify your validator code to: 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;
    }
}

and if your phalcon version is 2.0.5 and your class is loaded but u can't see the validation error message add this method to your model class: 并且如果您的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;
}

did you check the whole function? 您检查了整个功能吗?

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

        // This line:
        return $this->validationHasFailed() != true;
 }

I think that you missed running validationHasFailed method. 我认为您错过了运行validationHasFailed方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM