簡體   English   中英

zf2日期元素不是必需的

[英]zf2 Date element not required

我對Zend Framework 2和Date元素有疑問。 我要存儲的屬性是DateOfBirth,但是此屬性可能為空。 例如,日期未知。 數據庫中的列允許為NULL。 附加到它的Doctrine類具有一個屬性,讓我們知道它允許null。 但是Zend Framework 2仍然給我這個錯誤:

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

即使我設置了必需的attribute = false,也設置了allow_empty = true,但沒有任何效果。

將其修飾為表單內嵌套字段集的成員。 嵌套如下所示:

  • 用戶管理表
    • 用戶(字段集)
      • 人(田野)
        • 出生日期(元素)

我試過的幾個例子:

表單未正確驗證Zend Framework 2

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

這是我目前正在使用的代碼。 希望你能看到我想念的東西。 我不知道它是否由於嵌套的事實而存在,但是其余的都完美,只有date元素給我帶來了麻煩。

用戶管理表

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

?>

'required'不是element的屬性,而是validator屬性。 該解決方案包括實現Zend \\ InputFilter \\ InputFilterProviderInterface

use Zend\InputFilter\InputFilterProviderInterface;
class UserManagementForm extends AbstractSbmForm implements InputFilterProviderInterface {
  public function __construct()
  {
   ... without change
  }
  public function getInputFilterSpecification()
  {
    return array(
      'DateOfBirth' => array(
        'name' => 'DateOfBirth',
        'required' => false,
      );
    );
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM