Radiobutton group, change event? - php

I have a group of radiobuttons generated by cakephp.
<input type="radio" name="data[hexInput]" id="HexInput1" value="h1">
<input type="radio" name="data[hexInput]" id="HexInput2" value="h2">
<input type="radio" name="data[hexInput]" id="HexInput3" value="h3">
Is there some way to check if one of the radiobuttons in the group was checked? By a change event or something?
I know how to do this with a specific button but not with the group. For a specific button i use this Js helper:
$this->Js->get('HexInput2')->event('change', $this->Js->request(array(
'controller' => 'designer',
'action' => 'test',
), array(
'update' => '#resultDiv',
'async' => true,
'method' => 'post',
'dataExpression' => true,
'data' => $this->Js->serializeForm(array(
'isForm' => false,
'inline' => true
))
))
);

Just create an onclick for each radio button. You could do something like this:
function update(val) {
alert("Radio button changed to " + val);
}
Then just tie it to each of the inputs like this:
<input onclick="update(this.value);" type="radio" name="data[hexInput]" id="HexInput1" value="h1">
<input onclick="update(this.value);" type="radio" name="data[hexInput]" id="HexInput2" value="h2">
<input onclick="update(this.value);" type="radio" name="data[hexInput]" id="HexInput3" value="h3">

I think you are looking for something like this
http://www.java2s.com/Code/JavaScript/Form-Control/FindingtheSelectedButtoninaRadioGroup.htm

Related

Changing Radio button Input template using CakePHP 3

I would like change structure but it does not work, it's always the same structure. How to solve my problem?
<?php
$default = 'wepay';
$options = array('wepay' => 'WePay', 'paypal' => 'Paypal', 'donorbank' => 'Donor Bank');
echo $this->Form->input('payment_method', [
'templates' => [
'radioWrapper' => '<label class="radio-inline">{{label}}</label>'],
'type' => 'radio',
'options' => $options,
'default' => $default
]);
?>
But this code does not generate output properly. I want to generate output like below:
<label class="radio-inline">
<input type="radio" name="payment_method" value="wepay" id="payment-method-wepay" checked="checked">WePay
</label>
<label class="radio-inline">
<input type="radio" name="payment_method" value="paypal" id="payment-method-paypal">Paypal
</label>
<label class="radio-inline">
<input type="radio" name="payment_method" value="donorbank" id="payment-method-donorbank">Donor Bank
</label>
My Code generate output like below:
<label class="radio-inline">
<label for="payment-method-wepay">
<input type="radio" name="payment_method" value="wepay" id="payment-method-wepay" checked="checked">WePay
</label>
</label>
<label class="radio-inline">
<label for="payment-method-paypal">
<input type="radio" name="payment_method" value="paypal" id="payment-method-paypal">Paypal
</label>
</label>
<label class="radio-inline">
<label for="payment-method-donorbank"><input type="radio" name="payment_method" value="donorbank" id="payment-method-donorbank">Donor Bank
</label>
</label>
Here one extra label field added. How can I remove this extra-label?
What you are showing there is pretty much the default output, except for the class attribute and the missing for attribute on the label. So what you want to modify most probably is the nestingLabel template, surely not the radioWrapper one:
'nestingLabel' =>
'{{hidden}}<label class="radio-label"{{attrs}}>{{input}}{{text}}</label>'
If you want to remove the for attribute, then you'll have to remove the {{attrs}} placeholder, that way no further attributes can be added to the label element.
Finally, solved my problem using #ndm hints. Final solution like this below
<?php
$default = 'wepay';
$options = array('wepay' => 'WePay', 'paypal' => 'Paypal', 'donorbank' => 'Donor Bank');
echo $this->Form->input('payment_method', [
'nestingLabel' =>
'{{hidden}}<label class="radio-inline"{{attrs}}>{{input}}{{text}}</label>',
'type' => 'radio',
'options' => $options,
'default' => $default
]);
?>
Hope this solution help others in future.

How to make custom Radio-Button labels in ZF2 forms?

I have a form with Radio-button:
$this->add([
'name' => 'time',
'options' => [
'value_options' => [
'0' => '9:00 - 12:00',
'1' => '12:00 - 16:00',
'2' => '16:00 - 19:00',
],
'label_attributes' => [
'class' => 'WW_OBJ_fm-label',
]
],
'type' => 'Radio'
]);
In the view I make the output like this:
<div>
<?php echo $this->formElement($form->get('time')); ?>
</div>
and get the output (formatted for readability):
<div>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="0"/>
9:00 - 12:00
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="1"/>
12:00 - 16:00
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="2"/>
16:00 - 19:00
</label>
</div>
But I need, that label text ist wrapped by a <span>:
<div>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="0"/>
<span class="WW_label-text">9:00 - 12:00</span>
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="1"/>
<span class="WW_label-text">12:00 - 16:00</span>
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="2"/>
<span class="WW_label-text">16:00 - 19:00</span>
</label>
</div>
What is the best way to achieve it?
I see three possible solutions for your problem.
1) Extend the Zend\Form\View\Helper\FormRadio class, overriding the renderOptions method, replicating almost entirely the one that you can find in Zend\Form\View\Helper\FormMultiCheckbox but maybe adding an option to pass optional attributes to the span element
2) Very subtle, but could save you writing some code: using the translator. Since the radio value options are translated, you could keep your values defined in the configuration but adding the span element in the transation
3) Do not use $this->formElement to display the element, but actually write all the html
A solution is to use labelOption 'disable_html_escape' :
$this->add([
'name' => 'time',
'options' => [
'value_options' => [
'0' => '<span class="WW_label-text">9:00 - 12:00</span>',
'1' => '<span class="WW_label-text">12:00 - 16:00</span>',
'2' => '<span class="WW_label-text">16:00 - 19:00</span>',
],
'label_attributes' => [
'class' => 'WW_OBJ_fm-label',
]
],
'type' => 'Radio'
]);
$element = $this->get('time');
$element->setLabelOptions(['disable_html_escape' => true]);

Cakephp Radio input within table through foreach

I want radio button within table, without label and one radio button for one table row, while all grouped in one single group, so user can select single table row by selecting one radio button from the group ...
In pure html it looks like this:
<div class="radio">
<label>
<input type="radio" name="memberPaymentsRadio" value="mp<?php echo $key;?>" aria-label="...">
</label>
</div>
What would be the code in cakephp to get the same html code?
I tried like this:
foreach($payments as $key => $payment){
...
echo $form->input('memberPaymentsRadio',
array(
'div' => array('class' => 'radio'),
'label' => false,
'type' => 'radio',
'aria-label' => '...',
'options' => array('mp'.$key),
'before' => '<label>',
'after' => '</label>'
)
);
}
but I'm getting radio buttons, one per table row, and all within specific table column, but with labels 'mp0', 'mp1', which is not what I was looking for...
You can use like this
$this->Form->radio('memberPaymentsRadio', array('mp'.$key => ""),array('class' => 'radio', 'before' => '<label>','after' => '</label>','label' => false));

cakephp generates wrong label for radio input id

When i create a form on CakePHP with radio inputs, the label generated not match with the id of the radio input, the label "for" duplicates the name of the form. Here is my code:
echo $this->Form->create(
'test',
array(
'action' => 'index',
'type' => 'post',
'class' => 'fill-up',
'inputDefaults' => array('div' => 'input')));
$options = array('option1' => '1',
'option2' => '2');
$attributes = array('legend' = > false);
echo $this->Form->radio('Type', $options, $attributes);
echo $this->Form->end(
array(
'label' = > 'end',
'class' = > 'button',
'div' = > false));
and the generated HTML is something like:
<input type="hidden" name="data[test][options]" id="testOptions_" value="">
<input type="radio" name="data[test][options]" id="TestOptionsOption1" value="option1">
<label for="testTestOptionsOption1">1</label>
<input type="radio" name="data[test][options]" id="TestOptionsOption2" value="option2">
<label for="testTestOptionsOption2">2</label>
as you can see, cake duplicate the form name "test" on the label. How I can fix this? I try with the exact code of the documentations and still have the same issue
hope you can help me, thx very much
I auto-answer my question. That was a bug of cakephp, solved on the last version:
https://github.com/cakephp/cakephp/releases/tag/2.4.2
Try using
'label' => array(
'class' => 'thingy',
'text' => 'The User Alias'
)

Cakephp - Checkbox names

Currently, i have succeeded in creating the checkbox.
The array which i have setup is as below:
$emailName = $this->User->find('list', array(
'fields' => array('User.username', 'User.email')
));
The output is as follows:
array
'admin' => string 'asd#asd.asd' (length=11)
'test' => string 'test#test.test' (length=14)
'Floo' => string 'XXXX#gmail.com' (length=16)
I'm trying to make the checkbox shows the username instead of the user email in view.ctp.
I have tried using the following code in view.ctp
<?php echo $this->Form->input('Address_list.['.$emailName['username'].']', array(
'type' => 'select',
'multiple' => 'checkbox',
'options' => $emailName['email']
)); ?>
However, it seems that this doesn't work. Any ideas?
You are not formatting your form field for the checkbox list correctly. Try changing it to this:
echo $this->Form->input('Address_list', array(
'multiple' => 'checkbox',
'options' => $emailName,
));
However, this will return the value of Username based on the selection of Email the user chooses. It creates the form like this:
<label for="Address_list">Address List</label>
<input type="hidden" id="Address_list" value="" name="data[Address_list]"/>
<div class="checkbox"><input type="checkbox" id="AddressListAdmin" value="admin"
name="data[Address_list][]"/><label for="AddressListAdmin">asd#example.com</label></div>
<div class="checkbox"><input type="checkbox" id="AddressListTest" value="test"
name="data[Address_list][]"/><label for="AddressListTest">test#example.com</label></div>
<div class="checkbox"><input type="checkbox" id="AddressListFloo" value="Floo"
name="data[Address_list][]"/><label for="AddressListFloo">XXX#example.com</label></div>

Categories