douzhi4311 2013-07-19 14:55
浏览 17
已采纳

ZF2 - 控制/自定义单个收音机/复选框

I'm attempting to add data- values on radio buttons within ZF2. Is it possible to control each of the inputs specified with value_options?

A typical radio button added to a form:

$this->add(array(
    'type'  => 'radio',
    'name'  => 'duration',
    'options' => array(
        'value_options' => array(
            'daily' => 'Daily',
            'weekly' => 'Weekly',
            'monthly' => 'Monthly',
        ),
    ),
));  

Ultimately, I would like something like the following, so I can specify individual parameters/options for each radio item:

$this->add(array(
    'type'  => 'radio',
    'name'  => 'duration',
    'options' => array(
        'value_options' => array(
            array(
                'attributes' => array(
                    'value' => 'daily',
                    'data-item' => 'apple'
                ),
                'options' => array(
                    'label' => 'Daily'
                )
            ),
            array(
                'attributes' => array(
                    'value' => 'weekly',
                    'data-item' => 'orange'
                ),
                'options' => array(
                    'label' => 'Weekly'
                )
            ),
            array(
                'attributes' => array(
                    'value' => 'monthly',
                    'data-item' => 'pear'
                ),
                'options' => array(
                    'label' => 'Monthly'
                )
            ),
        ),
    ),
));

My reason for wanting the above, is that I want to use JavaScript to change something upon selecting a radio button, so it needs to hold data attributes.

Is anything like this possible yet?

  • 写回答

1条回答 默认 最新

  • doushenxu7294 2013-07-19 15:23
    关注

    It can be done by providing an array (or an object which implements ArrayAccess) instead of a single value (almost as you wrote in your example).

    $this->add(array(
        'type'  => 'radio',
        'name'  => 'duration',
        'options' => array(
            'value_options' => array(
                'daily' => array(
                    'label' => 'Daily',
                    'value' => 'daily',
                    'attributes' => array(
                        'data-item' => 'apple',
                    ),
                ),
                'weekly' => array(
                    'label' => 'Weekly',
                    'value' => 'weekly',
                    'attributes' => array(
                        'data-item' => 'orange',
                    ),
                ),
                'monthly' => array(
                    'label' => 'Monthly',
                    'value' => 'monthly',
                    'attributes' => array(
                        'data-item' => 'pear',
                    ),
                ),
            ),
        ),
    ));
    

    https://github.com/zendframework/zf2/blob/master/library/Zend/Form/View/Helper/FormMultiCheckbox.php#L177

    This should work on radios, multi-checkboxes & selects too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。