dongweishi2028 2015-09-15 12:03
浏览 13
已采纳

ZF2 - 始终需要输入类型“数字”?

I have this code in my Form.php

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

And this in my view.phtml

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

And when I try to submit the form, I have this error:

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

I already tried to set "required => false".

And if I change the type to TEXT instead of NUMBER, it works.

But why can't I use the type number? It seem to always be required...

  • 写回答

1条回答 默认 最新

  • douhan8581 2015-09-15 12:24
    关注

    If you look at the source of of zend-framework/zend-form/src/Element/Number.php you can see that this field is being set to required by default.

    /**
     * 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(),
        );
    }
    

    So you need to do something like this

    public function getInputFilterSpecification()
    {
        return [
            [
                "name"=>"unidades_andar",
                'required' => false,
                'allow_empty' => true, // this will allow submitting empty values like ''
            ],
        ];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)