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 ''
            ],
        ];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据