ZF2:允许空字段集,但验证是否至少填写了一个 [英] ZF2: allow empty fieldset, but validate if least one is filled out
问题描述
我为电话号码定义了一个字段集.这包含字段类型"(私人、Office 移动...)和号码".数字的输入过滤器是required => true":
I have defined a fieldset for phone numbers. This contains fields "type" (private, Office mobile ...) and "number". The Input filter for number is "required => true":
``
class PhoneFieldset extends BaseFieldset implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('phones');
$this->setHydrator(new DoctrineHydrator($this->getEntityManager(), 'HtsBase\Entity\Phone'))
->setObject(new Phone());
$this->add(array(
'type' => 'DoctrineORMModule\Form\Element\EntitySelect',
'name' => 'type',
'options' => array(
'label' => 'Type',
'empty_option' => '',
'object_manager' => $this->getEntityManager(),
'target_class' => 'HtsBase\Entity\OptionlistPhoneType',
'property' => 'name',
),
'attributes' => array(
#'id' => 'type',
'class' => 'input-medium',
),
));
$this->add(array(
'name' => 'number',
'options' => array(
'label' => 'Number',
),
'attributes' => array(
'type' => 'text',
#'id' => 'number',
'class' => 'input-medium',
'maxlength' => '25',
'autocomplete' => 'off',
),
));
}
public function getInputFilterSpecification()
{
return array(
'type' => array(
'required' => false,
),
'number' => array(
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'max' => 25,
),
),
),
),
);
}
``
我可以将验证器/过滤器附加到整个字段集吗?因此,如果类型"和数字"为空,则字段集有效,但验证是否至少填写了一个?
Can i attach a validator/filter to the entire fieldset? So that if "type" AND "number" are empty the fieldset is valid, but validate if least one is filled out?
推荐答案
我找到了一个易于使用的解决方案,虽然我不再使用表单了,但我现在大量使用 InputFilter
并且仍然需要同样的东西.但找到了一个简单的解决方案
I found an easy to use solution, although I don't use the form any more I now heavily use the InputFilter
and still needed the same stuff. But found an easy solution
AbstractFilterValidator
,我自己的实现
abstract class AbstractFilterValidator extends AbstractValidator
{
/**
* Returns true if and only if $value meets the validation requirements
*
* If $value fails validation, then this method returns false, and
* getMessages() will return an array of messages that explain why the
* validation failed.
*
* @param mixed $value
* @return bool
* @throws Exception\RuntimeException If validation of $value is impossible
*/
public function isValid($value)
{
$this->setValue($value);
$filter = $this->buildFilter();
$filter->setData($value);
if (!$filter->isValid()) {
$this->abstractOptions['messages'] = $filter->getMessages();
return false;
}
return true;
}
/**
* @return array
*/
public function getMessages()
{
return $this->abstractOptions['messages'];
}
/**
* @return InputFilter\InputFilter
*/
abstract protected function buildFilter();
}
<小时>
旧答案
虽然你使用了 InputFilterProviderInterface
,但我使用了 Zend\InputFilter\InputFilter
并且想要和你一样的.如果未填写字段集,则验证 true
.
Although you were using the InputFilterProviderInterface
, I used Zend\InputFilter\InputFilter
and wanted the same as you. If the fieldset was not filled in, validate true
.
为此,我将 isValid
替换为以下内容;
To do this I replace isValid
with the following;
public function isValid()
{
$values = array_filter($this->getRawValues());
if (empty($values)) {
return true;
}
return parent::isValid();
}
它只是从所有空数组键中过滤数组,参见 docs 有关这方面的信息.然后检查 $values
是否为空,如果是,则返回 true
.否则将调用验证器.
It simply filters the array from all empty array keys, see docs for info about that. Then a check if the $values
is empty, and if so return true
. Else the validators are called.
好吧,我又需要一些东西,但需要一个不错的解决方案.还是没有找到好的,所以我写了下面的代码.
Well I needed something again but needed a decent solution. Still no luck finding a good one, so I wrote the following code.
<?php
namespace Application\InputFilter;
use Zend\InputFilter as ZFI;
class InputFilter extends ZFI\InputFilter
{
private $required = true;
/**
* @return boolean
*/
public function isRequired()
{
return $this->required;
}
/**
* @param boolean $required
*
* @return $this
*/
public function setRequired($required)
{
$this->required = (bool) $required;
return $this;
}
/**
* @return bool
*/
public function isValid()
{
if (!$this->isRequired() && empty(array_filter($this->getRawValues()))) {
return true;
}
return parent::isValid();
}
}
您现在可以简单地在 InputFilter
这篇关于ZF2:允许空字段集,但验证是否至少填写了一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!