[英]Require at least one element in ZF2 FieldSet
问题
我有一个Form
和一个FieldSet
。 我想验证FieldSet
是否为空。 另外,我想验证FieldSet
每个字段。
到目前为止,我所尝试的是验证一个或另一个,而不是两个。 如果elements
存在于所述表格的输入滤波器说明书中,然后它验证elements
不是空的,但不验证bar
和baz
领域FieldSet
。 当然,反之亦然。 任何有关如何解决此问题的线索将不胜感激。
表格
class FooForm extends Form implements InputFilterProviderInterface
{
public function init()
{
$this->add([
'name' => 'elements',
'type' => Collection::class,
'required' => true,
'options' => [
'target_element' => [
'type' => SomeElementFieldSet::class
]
]
]);
}
public function getInputFilterSpecification()
{
return [
[
'name' => 'elements',
'required' => true,
'validators' => [
['name' => 'NotEmpty']
]
]
];
}
}
FieldSet
class SomeElementFieldSet extends Fieldset implements InputFilterProviderInterface
{
public function init()
{
$this->add(['name' => 'bar']);
$this->add(['name' => 'baz']);
}
public function getInputFilterSpecification()
{
return [
[
'name' => 'bar',
'required' => true,
'validators' => [
['name' => 'NotEmpty']
]
],
[
'name' => 'baz',
'required' => true,
'validators' => [
['name' => 'NotEmpty']
]
]
];
}
}
编辑 :添加了完整的验证规范。
在获得关于Google的一些提示并仔细研究了源代码之后,我找到了解决方案。 不幸的是, zend-inputfilter
实现有点错误,不能与getInputFilterSpecification()
很好地配合,但是我们可以构造自己的InputFilter
并直接返回它:
表格
class FooForm extends BaseForm
{
public function init()
{
$this->add([
'name' => 'elements',
'type' => Collection::class,
'options' => [
'target_element' => [
'type' => SomeElementFieldSet::class
]
]
]);
}
public function getInputFilter()
{
if (!$this->filter) {
$this->filter = new InputFilter();
/** @var Collection $elementsCollection */
$elementsCollection = $this->fieldsets['elements'];
/** @var SomeElementFieldSet $elementsFieldSet */
$elementsFieldSet = $elementsCollection->getTargetElement();
$collectionFilter = new CollectionInputFilter();
$collectionFilter->setIsRequired(true);
$collectionFilter->setInputFilter(
$elementsFieldSet->getInputFilterSpecification()
);
$this->filter->add($collectionFilter, 'elements');
}
return $this->filter;
}
}
这将验证集合中至少有一个元素。 并将按照FieldSet的规范逐一验证所有元素。
但是,仍然存在一个问题。 每当集合为空时,验证将返回false
,但不会返回任何消息。 这是由于zend-inputfilter
组件中的错误所致。 这里和这里报道的问题。 但这完全是另一个问题。
通过指定要验证的输入字段数组,在Form
对象中使用setValidationGroup()
方法。 请参考文档 !
您可以这样尝试 。 尽管我在表单中添加了一些额外的字段,仅用于测试目的。
class FooForm extends Form implements InputFilterProviderInterface
{
public function __construct($name = null, $options = array())
{
parent::__construct($name, $options);
$this->add(['name' => 'title']);
$this->add([
'name' => 'elements',
'type' => Collection::class,
'required' => true,
'options' => [
'target_element' => [
'type' => SomeElementFieldSet::class,
],
],
]);
$this->add([
'type' => 'submit',
'name' => 'submit',
'attributes' => [
'value' => 'Post'
],
]);
// I pointed this. Here you can specify fields to be validated
$this->setValidationGroup([
'title',
'elements' => [
'bar',
],
]);
}
public function getInputFilterSpecification()
{
return [
[
'name' => 'title',
'required' => true,
'validators' => [
['name' => 'NotEmpty']
]
],
];
}
}
您的fieldset类应该是
class SomeElementFieldSet extends Fieldset implements InputFilterProviderInterface
{
public function init()
{
$this->add(['name' => 'bar']);
$this->add(['name' => 'baz']);
}
public function getInputFilterSpecification()
{
return [
[
'name' => 'bar',
'required' => true,
'validators' => [
['name' => 'NotEmpty']
]
],
[
'name' => 'baz',
'required' => true,
'validators' => [
['name' => 'NotEmpty']
]
]
];
}
}
希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.