zf2不需要日期元素

时间:2016-01-10 18:56:19

标签: php zend-framework2 zend-inputfilter

我遇到Zend Framework 2和Date元素的问题。我尝试存储的属性是DateOfBirth,但此属性可能为空。例如,日期未知。数据库中的列允许NULL。附加到它的Doctrine类有一个属性,让它知道它允许null。但Zend Framework 2仍然给我这个错误:

"Value is required and can't be empty".

即使我设置了required = false,也设置了allow_empty = true,但没有任何效果。

attirbute它是表单中嵌套字段集的成员。嵌套看起来如下:

  • UserManagementForm
    • 用户(字段集)
      • 人(字段集)
        • DateOfBirth(element)

我试过几个例子:

Form not validating correctly zend framework 2

https://github.com/zendframework/zf2/issues/4302

这是我目前正在使用的代码。希望你能看到我失踪的东西。我不知道它是否由于它是嵌套的,但其余的工作完美,只有日期元素给我带来了麻烦。

UserManagementForm

<?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',
            ),
        ));
    }
}

?>

1 个答案:

答案 0 :(得分:3)

'required'不是元素的属性,而是validator属性。 解决方案包括实现Zend \ InputFilter \ InputFilterProviderInterface

+(instancetype)sharedStore{
   static BNRItemStore*sharedStore=nil;
    if(!sharedStore){
        sharedStore=[[self alloc]initPrivate];
    }
    return sharedStore;
}