我在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')); ?>
当我尝试提交表单时,我有这个错误:
数组([unidades_andar] =&gt;数组([isEmpty] =&gt;数值是必需的, 不能空了))
我已经尝试设置&#34; required =&gt;假&#34;
如果我将类型更改为TEXT而不是NUMBER,则可以正常工作。
但为什么我不能使用型号?它似乎总是需要......
答案 0 :(得分:2)
如果查看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 ''
],
];
}