过滤器的必需选项停用 ZF3 中的验证

Required option of the filter deactivates validation in ZF3

在 ZF3 中,我创建了一个包含两个字段的表单:文本和 url。用户只能填写其中一项,且至少必须填写一项。

想象一下:一个人可以把网站的内容或网站的url。该表单可用于从网站或文本中获取某些数据。

我准备了两个验证器 classes。每个输入一个。 classes 正在从上下文参数中获取另一个的输入值。 StringLength 验证器用于两个字段。

这几乎可以正常工作,但是当两个字段都提交为空时,问题就来了。然后数据确实通过了验证,而它应该没有。

在这个问题的情况下,字段 required 变成了 false。

当我将它们切换为 true 时,两个字段都是必需的,但我只希望一个是必需的。

因此,目标是当两个字段都为空时,验证结果将为假。然后应该出现唯一的一条消息。我的意思是消息或多或少是这样的:One of fields must be filled out. 不是 'required' 消息。

这是表单 class 和两个验证器 classes.

<?php

namespace Application\Filter;

use Application\Form\Test as Form;
use Application\Validator\Text;
use Application\Validator\Url;
use Zend\InputFilter\InputFilter;

class Test extends InputFilter
{
    public function init()
    {
        $this->add([
            'name' => Form::TEXT,
            'required' => false,
            'validators' => [
                ['name' => Text::class],
            ],
        ]);
        $this->add([
            'name' => Form::URL,
            'required' => false,
            'validators' => [
                ['name' => Url::class],
            ],
        ]);
    }
}

<?php

namespace Application\Validator;

use Zend\Validator\StringLength;
use Zend\Validator\ValidatorInterface;

class Text implements ValidatorInterface
{
    protected $stringLength;
    protected $messages = [];

    public function __construct()
    {
        $this->stringLengthValidator = new StringLength();
    }

    public function isValid($value, $context = null)
    {
        if (empty($context['url'])) {
            $this->stringLengthValidator->setMin(3);
            $this->stringLengthValidator->setMax(5000);

            if ($this->stringLengthValidator->isValid($value)) {
                return true;
            }
            $this->messages = $this->stringLengthValidator->getMessages();

            return false;
        }
        if (!empty($value)) return false;
    }

    public function getMessages()
    {
        return $this->messages;
    }
}

<?php

namespace Application\Validator;

use Zend\Validator\StringLength;
use Zend\Validator\ValidatorInterface;

class Url implements ValidatorInterface
{
    const ERROR_NOT_ALLOWED_STRING = 'string-not-allowed';
    protected $stringLength;
    protected $messages = [
        self::ERROR_NOT_ALLOWED_STRING => 'Only one of text and url field may by filled.',
    ];

    public function __construct()
    {
        $this->stringLengthValidator = new StringLength();
    }

    public function isValid($value, $context = null)
    {
        if (empty($context['text'])) {
            $this->stringLengthValidator->setMin(3);
            $this->stringLengthValidator->setMax(500);

            if ($this->stringLengthValidator->isValid($value)) {
                return true;
            }
            $this->messages = $this->stringLengthValidator->getMessages();

            return false;
        }
        if (!empty($value)) return false;
    }

    public function getMessages()
    {
        return $this->messages;
    }
}

更新

我使用了@Crisp 的建议,不得不在代码中做一些更正。添加了 returns 和消息处理。工作代码如下:

<?php

namespace Application\Filter;

use Application\Form\Test as Form;
use Application\Validator\Text;
use Application\Validator\Url;
use Zend\InputFilter\InputFilter;

class Test extends InputFilter
{
    public function init()
    {
        $this->add([
            'name' => Form::TEXT,
            'required' => false,
            'allow_empty' => true,
            'continue_if_empty' => true,
            'validators' => [
                ['name' => Text::class],
            ],
        ]);
        $this->add([
            'name' => Form::URL,
            'required' => false,
            'allow_empty' => true,
            'continue_if_empty' => true,
            'validators' => [
                ['name' => Url::class],
            ],
        ]);
    }
}

<?php

namespace Application\Validator;

use Zend\Validator\StringLength;
use Zend\Validator\ValidatorInterface;

class Text implements ValidatorInterface
{
    protected $stringLength;
    protected $messages = [];

    public function __construct()
    {
        $this->stringLengthValidator = new StringLength();
    }

    public function isValid($value, $context = null)
    {
        if (empty($context['url'])) {
            if (empty($value)) return false;
            $this->stringLengthValidator->setMin(3);
            $this->stringLengthValidator->setMax(5000);

            if ($this->stringLengthValidator->isValid($value)) {
                return true;
            }
            $this->messages = $this->stringLengthValidator->getMessages();

            return false;
        }
        if (!empty($value)) return false;
        return true;
    }

    public function getMessages()
    {
        return $this->messages;
    }
}

<?php

namespace Application\Validator;

use Zend\Validator\StringLength;
use Zend\Validator\ValidatorInterface;

class Url implements ValidatorInterface
{
    const ERROR_NOT_ALLOWED_STRING = 'string-not-allowed';
    const ERROR_EMPTY_FIELDS = 'empty-fields';
    protected $stringLength;
    protected $messages = [
        self::ERROR_NOT_ALLOWED_STRING => 'Only one of text and url field may be filled out.',
    ];

    public function __construct()
    {
        $this->stringLengthValidator = new StringLength();
    }

    public function isValid($value, $context = null)
    {
        if (empty($context['text'])) {
            if (empty($value)) {
                $this->messages = [
                    self::ERROR_EMPTY_FIELDS => 'One of the fields must be filled out.',
                ];
                return false;
            }
            $this->stringLengthValidator->setMin(3);
            $this->stringLengthValidator->setMax(500);

            if ($this->stringLengthValidator->isValid($value)) {
                return true;
            }
            $this->messages = $this->stringLengthValidator->getMessages();
            return false;
        }
        if (!empty($value)) return false;
        return true;
    }

    public function getMessages()
    {
        return $this->messages;
    }
}

为确保您的验证器始终 运行,即使对于空值,您也需要将 allow_emptycontinue_if_empty 选项添加到您的输入规范中。否则将跳过任何不是 required.

的值的验证

以下组合应该有效

class Test extends InputFilter
{
    public function init()
    {
        $this->add([
            'name' => Form::TEXT,
            'required' => false,
            'allow_empty' => true,
            'continue_if_empty' => true,
            'validators' => [
                ['name' => Text::class],
            ],
        ]);
        $this->add([
            'name' => Form::URL,
            'required' => false,
            'allow_empty' => true,
            'continue_if_empty' => true,
            'validators' => [
                ['name' => Url::class],
            ],
        ]);
    }
}

该组合应确保在遇到空值时应用您的验证器。

Rob Allen (@akrabat) 写了一篇有用的博客 post 详细介绍了值得收藏的组合 akrabat.com/zend-input-empty-values/