所以我有一个“简单”的表格
class SiteAddForm extends Form
{
public function __construct()
{
parent::__construct('add_site_form');
$site = new SiteFieldSet();
$this->add($site);
}
public function getTemplate()
{
return 'site_add.phtml';
}
}
它自己的形式什么也不做。它添加一个 field_set 并返回一个模板名称。
SiteFieldSet 看起来像:
class SiteFieldSet
extends FieldSet
implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('site');
$name = new Text('name');
$this->add($name);
$domains = new Collection('domains');
$domains->setTargetElement(new DomainFieldSet())
->setShouldCreateTemplate(true);
$this->add($domains);
}
public function getTemplate()
{
return 'site.phtml';
}
/**
* Should return an array specification compatible with
* {@link Zend\InputFilter\Factory::createInputFilter()}.
*
* @return array
*/
public function getInputFilterSpecification()
{
return [
'name' => [
'required' => true,
'validators' => [
new StringLength([
'min' => 200,
])
]
],
'domains' => [
'required' => true,
],
];
}
}
它将文本和集合元素添加到字段集中。字段集实现InputFilterProviderInterface
验证投入其中的数据。
名称必须至少为 200 个字符(用于测试)并且需要集合。
但现在我的问题来了。使用被扔到集合中的字段集,代码:
class DomainFieldSet
extends FieldSet
implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('domain');
$host = new Url('host');
$this->add($host);
$language = new Select('language', [
'value_options' => [
'nl_NL' => 'NL',
],
]);
$this->add($language);
$theme = new Select('theme', [
'value_options' => [
'yeti' => 'Yeti',
]
]);
$this->add($theme);
}
public function getTemplate()
{
return 'domain.phtml';
}
/**
* Should return an array specification compatible with
* {@link Zend\InputFilter\Factory::createInputFilter()}.
*
* @return array
*/
public function getInputFilterSpecification()
{
return [
'host' => [
'required' => true,
'validators' => [
new StringLength([
'min' => 200,
])
]
],
'language' => [
'required' => true,
],
'theme' => [
'required' => true,
],
];
}
}
再次没什么特别的。现在定义了主机、主题和语言三个元素。字段集再次实现InputFilterProviderInterface
。所以类中必须有一个getInputFilterSpecification。
当我填写表格时
site[name]
= "test"
site[domains][0][host]
= 'test'
site[domains][0][theme]
=
site[domains][0][language]
'yeti' = 'nl_NL'
它给站点 [名称] 一个错误,说它必须至少 200 个字符,所以验证“有效”但它也应该给出一个错误site[domains][0][host]
,它需要至少 200 个字符(代码是复制粘贴的,并且使用是正确的) .
那么为什么不开始验证,或者我该如何解决这个问题,以便正确验证集合中的元素/字段集