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));
Related
I have a dropDownList like follwing:
<?php echo TbHtml::dropdownList('cls', $sel, CHtml::listData($classes, 'type_class', 'name_class')); ?>
That generates a select option like that:
<select id="cls" name="cls">
<option value="1">Main Class</option>
<option value="2">Other Class</option>
</select>
When I click on a button, I need to get the value, in my example 1 or 2.
The button is built like following:
echo $this->widget('booster.widgets.TbButton', array(
'buttonType' => 'link',
'context' => 'warning',
'icon' => 'fa fa-file-pdf-o',
'label' => 'Submit',
'size' => 'small',
'url' => array(
'//wseder/excel/wws',
'code_from_select' => $VALUE_THAT_I_NEED // here i need the value
),
'htmlOptions' => array(
'class' => 'pull-rigth btn-nxt-prv',
'style' => 'margin-left:5px'
)
), true);
How can I access to the value selected from the select?
How I understand you creating select on html page and button on html page. When you click button you need get select val and put it in button. If so, you can not do it, because php do not have access to select val on html page. You should use javascript (jquery) to read select value and insert it where you need.
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'
)
Inside my form there is a long row of checkboxes which I want to show as two columns of checkboxes (for reasons related to presentation). So in the following code I split the options into two separate arrays and create two different options by the same name. When I debug($this->request->data); the 'location' key is always empty. However, the same code works as a single input just fine. What am I doing wrong?
<?php
$count = count($location_options); //$location_options is passed from the controller
$half = round( $count/2 );
$location_options1 = array_slice($location_options, 0, $half, TRUE);
$location_options2 = array_slice($location_options, $half, NULL, TRUE);
//I CAN'T GET THIS TO WORK!!
//echo $this->Form->input('location', array('type'=>'select', 'multiple'=>'checkbox', 'options'=>$location_options1, 'div'=>array('class'=>'col-xs-12 col-sm-6 form-group', 'style'=>'margin-bottom:0;', 'selected'=>$user_location_alert_tag_ids)));
//echo $this->Form->input('location', array('type'=>'select', 'multiple'=>'checkbox', 'options'=>$location_options2, 'div'=>array('class'=>'col-xs-12 col-sm-6 form-group', 'selected'=>$user_location_alert_tag_ids)));
//BUT THIS WORKS JUST FINE
echo $this->Form->input('location', array('type'=>'select', 'multiple'=>'checkbox', 'options'=>$location_options, 'div'=>array('selected'=>$user_location_alert_tag_ids)));
?>
Look at the generated HTML, for each select element a hidden field is generated that ensures that the appropriate key will be present in the data.
Multiple fields with the same name will cause multiple hidden fields to be generated where the last one will overwrite the former ones.
This can be avoided using the hiddenField option for the additional fields, so that the hidden initializer field is only generated for the first input. Quote from the docs:
If you want to create multiple blocks of inputs on a form that are all grouped together, you should use this parameter on all inputs except the first. If the hidden input is on the page in multiple places, only the last group of input’s values will be saved.
Also you should define a unique ID for both inputs, otherwise you'll end up with invalid HTML as the helper will produce duplicate IDs.
And last but not least your parentheses are probably a little wrong, the selected key is nested in the div key, which I guess is wrong in case this is ment to define the selected entries.
echo $this->Form->input('location', array(
'id' => 'location1',
'type' => 'select',
'multiple' => 'checkbox',
'options' => $location_options1,
'div' => array('class' => 'col-xs-12 col-sm-6 form-group', 'style'= > 'margin-bottom:0;')
'selected' => $user_location_alert_tag_ids
)));
echo $this->Form->input('location', array(
'id' => 'location2',
'type' => 'select',
'multiple' => 'checkbox',
'options' => $location_options2,
'div' => array('class' => 'col-xs-12 col-sm-6 form-group'),
'selected' => $user_location_alert_tag_ids
'hiddenField' => false
)));
I create 2 arrays from a model, 1 being already selected values from the user, and the 2nd available values which the user can then also select. This is for an edit page. I want to populate a multi-select input box with the values of both models, but want the already chosen values (1st array) highlighted. It creates the models fine, and using array_merge() I merge both the arrays as the options, but the selected does not highlight the correct fields. Any tips?
// Controller:
$ivrNumbersAvail = $this->Survey->SurveyIvrNumber->find("list",array("conditions" => array("OR" => array("SurveyIvrNumber.survey_id" => array($id)))));
$ivrNumbersSelected = $this->Survey->SurveyIvrNumber->find("list",array("conditions" => array("OR" => array("SurveyIvrNumber.survey_id" => array(0)))));
// In the view:
echo $this->Form->input('SurveyIvrNumbers.id',array(
'empty' => '-- Select IVR Number(s) --',
'options' => array_merge($ivrNumbersAvail,$ivrNumbersSelected),
'selected' => $ivrNumbersSelected,
'class' => 'textbox',
'multiple' => true,
'div' => array(
'class' => 'field'
),
'label' => array(
'class' => 'label-tooltip',
'title' => '', //tool tips
'text' => 'IVR Numbers: (you can select multiple numbers)'
),
'after' => '<p class="field-help"></p>'
));
If you set $this->request->data to the record you are currently editing CakePHP will automatically populate this data for you!
// CONTROLLER
// this line sets the data
$this->request->data = $this->Survey->read(null, $id);
// this passes the SurveyIvrNumbers to the view, (you can put any options on to this)
$this->set('SurveyIvrNumber',$this->Survey->SurveyIvrNumber->find('all'));
// VIEW
// CakePHP does the rest
echo $this->Form->input('SurveyIvrNumbers',array(
'empty' => '-- Select IVR Number(s) --', // plus other options
);
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>