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

时间:2018-03-11 04:39:44

标签: php validation filter zend-framework3

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

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

我准备了两个验证器类。每个输入一个。这些类从context参数获取另一个的输入值。 StringLength验证器用于两个字段。

这种方法几乎没问题,但是当两个字段都提交为空时,问题就出现了。然后数据确实通过了验证,而它应该没有。

如果出现此问题,则字段required会变为false。

当我将它们切换为true时,需要两个字段,但我只想要一个字段。

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

这里是表单类和两个验证程序类。

<?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的建议,并且必须在代码中进行一些修正。添加了返回和消息处理。工作代码如下:

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

1 个答案:

答案 0 :(得分:1)

为确保验证程序始终运行,即使是空值,也需要在输入规范中添加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)写了一篇有用的博客文章,详细介绍了值得加入书签的组合akrabat.com/zend-input-empty-values/