[英]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.