ZF2 - 始终需要输入类型 "number"?

ZF2 - Input type "number" always required?

我的 Form.php

中有此代码
$this->add(array(
    'name' => 'unidades_andar',
    'type' => 'number',
    'attributes' => array(
        'class' => 'form-control',
    ),
));

这在我的 view.phtml

<?php echo $this->formElement($form->get('unidades_andar')); ?>

当我尝试提交表单时,我遇到了这个错误:

Array ( [unidades_andar] => Array ( [isEmpty] => Value is required and can't be empty ) )

我已经尝试设置 "required => false"。

如果我将类型更改为 TEXT 而不是 NUMBER,它会起作用。

但为什么我不能使用型号?似乎总是需要...

如果您查看 zend-framework/zend-form/src/Element/Number.php 的来源,您可以看到该字段默认设置为必填。

/**
 * Provide default input rules for this element
 *
 * Attaches a number validator, as well as a greater than and less than validators
 *
 * @return array
 */
public function getInputSpecification()
{
    return array(
        'name' => $this->getName(),
        'required' => true,
        'filters' => array(
            array('name' => 'Zend\Filter\StringTrim')
        ),
        'validators' => $this->getValidators(),
    );
}

所以你需要做这样的事情

public function getInputFilterSpecification()
{
    return [
        [
            "name"=>"unidades_andar",
            'required' => false,
            'allow_empty' => true, // this will allow submitting empty values like ''
        ],
    ];
}