【发布时间】:2016-01-10 18:56:18
【问题描述】:
我对 Zend Framework 2 和 Date 元素有疑问。我要存储的属性是 DateOfBirth,但此属性可能为空。例如日期未知。数据库中的列允许 NULL。附加到它的 Doctrine 类有一个属性,让它知道它允许 null。但是 Zend Framework 2 仍然给我这个错误:
"Value is required and can't be empty".
即使我设置了必需的属性=false,也设置了allow_empty=true,但没有任何效果。
属性是表单内嵌套字段集的成员。嵌套如下:
- 用户管理表单
- 用户(字段集)
- 人员(字段集)
- DateOfBirth(元素)
- 人员(字段集)
- 用户(字段集)
我试过的几个例子:
Form not validating correctly zend framework 2
https://github.com/zendframework/zf2/issues/4302
这是我目前正在使用的代码。希望你能看到我遗漏的东西。我不知道是不是因为它是嵌套的,但其余的工作完美,只有日期元素给我带来了麻烦。
用户管理表单
<?php
namespace Application\Form;
use Zend\Form\Form;
class UserManagementForm extends Form
{
public function __construct()
{
parent::__construct('usermanagementform');
$this->setAttribute('method', 'post');
$fieldset = new \Application\Form\Fieldset\User();
$fieldset
->setHydrator(new \Zend\Stdlib\Hydrator\ObjectProperty(false))
->setObject(new \Application\Entity\User())
->setOptions(array('use_as_base_fieldset' => true))
;
$this->add($fieldset);
$this->add(array(
'name' => 'btnSubmit',
'type' => 'submit',
'attributes' => array(
'class' => 'btn-primary',
),
'options' => array(
'column-size' => 'sm-9 col-sm-offset-3',
'label' => 'Save changes',
),
));
}
}
?>
用户(字段集)
<?php
namespace Application\Form\Fieldset;
use Zend\Form\Fieldset;
class User extends Fieldset
{
public function __construct()
{
parent::__construct('User');
$fieldset = new \Application\Form\Fieldset\EmailAddress();
$fieldset
->setHydrator(new \Zend\Stdlib\Hydrator\ObjectProperty(false))
->setObject(new \Application\Entity\EmailAddress());
$this->add($fieldset);
$fieldset = new \Application\Form\Fieldset\Person();
$fieldset
->setHydrator(new \Zend\Stdlib\Hydrator\ObjectProperty(false))
->setObject(new \Application\Entity\Person());
$this->add($fieldset);
}
}
?>
人员(字段集)
<?php
namespace Application\Form\Fieldset;
use Zend\Form\Fieldset;
class Person extends Fieldset
{
public function __construct()
{
parent::__construct('Person');
$this->add(array(
'type' => 'date',
'name' => 'DateOfBirth',
'required' => false,
'allowEmpty' => true,
'options' => array(
'label' => 'Date of birth',
'column-size' => 'sm-9',
'label_attributes' => array(
'class' => 'col-sm-3',
),
'format' => 'd-m-Y',
),
));
}
}
?>
【问题讨论】:
-
您将输入过滤器规范/配置添加到
Form类。检查@AlainPomirol 他的答案!
标签: php zend-framework2 zend-inputfilter