【问题标题】:ZF2 - Input type "number" always required?ZF2 - 总是需要输入类型“数字”?
【发布时间】:2015-09-15 12:03:05
【问题描述】:

我的 Form.php 中有这段代码

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

这在我看来.phtml

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

当我尝试提交表单时,出现以下错误:

Array ( [unidades_andar] => Array ( [isEmpty] => 值是必需的,并且 不能为空))

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

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

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

【问题讨论】:

  • 是否有任何 InoutFilter 验证?如果是,请在此处发布代码。
  • 不,没有。如果我改变类型做“文本”而不​​是“数字”它可以工作......所以这不是过滤器的问题......我猜......

标签: php zend-framework zend-framework2


【解决方案1】:

如果您查看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 ''
        ],
    ];
}

【讨论】:

    猜你喜欢
    • 2021-05-01
    • 2018-12-31
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多