2012-11-01 26 views
1

我正在使用CakePHP 2.2.3生成一个radioset,然后应用jQuery buttonset。jQuery buttonset和带有隐藏值的CakePHP收音机设置

CakePHP的片段:

echo $this->Form->input('contact_address_type', array(
    'type' => 'radio', 
    'legend' => false, 
    'options' => array('domestic' => 'Domestic', 'po_box' => 'Domestic PO Box', 'foreign' => 'Foreign'), 
    'div' => array('class' => 'contact address_type') 
)); 

jQuery的UI片段:

$('.contact.address_type').buttonset(); 

当生成HTML,还有一个隐藏字段共享相同的名称作为无线电集。这样做是为了当提交表单时,即使未选择radiume,我仍然会在我的请求 - >数据中得到一个值(虽然是空白的)。

但是,当选择其中一个选项时,jQuery UI按钮设置代码并不喜欢这样做,因为它期望所有共享表单中选定广播选项名称的元素都具有与它们关联的按钮小部件。在这一点上,associate jQuery UI ticket是wontfix。

我不愿意向CakePHP提交一张票,因为有一个隐藏的字段与默认值相同的名称为非选定的收音机是一种合法的做法。

这个jsFiddle显示了一个例子。您需要打开您的JavaScript控制台才能在选择按钮时查看生成的错误。

回答

0

一个可能的答案是生成一个没有标签的假收音机选项。

这将防止它被包含在呈现的按钮集中,但它仍然获得附加到它的按钮对象。您还需要将收音机默认为选中状态,以便如果没有选择其他按钮,则会提交该默认值。

echo $this->Form->input('contact_address_type', array(
    'type' => 'radio', 
    'legend' => false, 
    'options' => array('domestic' => 'Domestic', 'po_box' => 'Domestic PO Box', 'foreign' => 'Foreign'), 
    'value' => false, 
    'before' => $this->Form->radio('contact_address_type', array('' => ''), array('legend' => false, 'checked' => 'checked', 'value' => false, 'label' => false)), 
    'div' => array('class' => 'contact address_type') 
)); 

另一个选项是强制用户的默认选择,所以你知道会有一个选项被选中。

echo $this->Form->input('contact_address_type', array(
    'type' => 'radio', 
    'legend' => false, 
    'options' => array('domestic' => 'Domestic', 'po_box' => 'Domestic PO Box', 'foreign' => 'Foreign'), 
    'value' => 'domestic', 
    'div' => array('class' => 'contact address_type') 
)); 

但我想看看是否有更好的选择。

相关问题