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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器