Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 过滤器的必需选项将停用ZF3中的验证_Php_Validation_Filter_Zend Framework3 - Fatal编程技术网

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

Php 过滤器的必需选项将停用ZF3中的验证,php,validation,filter,zend-framework3,Php,Validation,Filter,Zend Framework3,在ZF3中,我创建了一个包含两个字段的表单:文本和url。用户只能填写其中一项,并且必须至少填写一项 想象一下:你可以把网站的内容或网站的url。该表单可用于从网站或文本中获取某些数据 我准备了两个验证器类。每个输入一个。这些类从上下文参数获取另一个类的输入值。这两个字段都使用了StringLength验证程序 这几乎可以正常工作,但当两个字段都提交为空时,坏问题就出现了。然后数据确实通过了验证,而它应该不会通过 在出现此问题时,字段的required变为false 当我将它们切换为true时,

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

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

我准备了两个验证器类。每个输入一个。这些类从上下文参数获取另一个类的输入值。这两个字段都使用了StringLength验证程序

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

在出现此问题时,字段的
required
变为false

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

因此,目标是当两个字段都为空时,验证结果将为false。然后只显示一条消息。我的意思是消息或多或少是这样的:
必须填写其中一个字段。
不是“必需”消息

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

<?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],
            ],
        ]);
    }
}

为了确保验证器始终运行,即使对于空值,也需要在输入规范中添加
allow\u empty
continue\u if\u empty
选项。否则,将跳过任何不需要
的值的验证

以下组合应该有效

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)写了一篇有用的博客文章,详细介绍了值得收藏的组合

<?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;
    }
}
<?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;
    }
}
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],
            ],
        ]);
    }
}