CakePHP: FormHelper not saving data from two inputs with same name - php

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
)));

Related

FormHelper::input() creates a drop-down select if fielname has the "_id" suffix

I have the following line of code on a CakePHP view:
<?php
echo $this->Form->input(
'person_id',
array(
'label' => false,
'div' => false,
'class' => 'form-control search-person'
)
);
?>
I want to create a text input with this line of code, but if the field name has the suffix _id, the rendered HTML changes from a text field to a drop-down select.
If I change the prefix to anything else, for example person_idd or abc_idd, it renders a text input, but if the field name ends with _id suffix, it renders a drop-down select, which doesn't allow me to write anything.
Is this some CakePHP feature? How can I avoid this behavior and produce a text input with a field ending with the _id suffix?
It's a CakePHP feature:
This method will automatically inspect the model field it has been supplied in order to create an appropriate input for that field.
Taken from Cookbook 2.x: FormHelper: Creating form elements.
To get a text input, add 'type' => 'text' to the options array:
<?php echo $this->Form->input('person_id', array(
'type' => 'text',
'label' => false,
'div' => false,
'class' => 'form-control search-person'
)); ?>

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));

How to add html/text into a zend form htmlTag?

I am trying to inset text into a html tag created using zend form.
This is my code below, excluding my actual form elements
$this->addElements(array(
// My other form elements
array(
'hidden',
'dummy',
array(
'required' => false,
'ignore' => true,
'autoInsertNotEmptyValidator' => false,
'decorators' => array(
array(
'HtmlTag', array(
'tag' => 'div',
'id' => 'DescCharsRemaining',
'setValue' => '
2000 Characters Remaining
'
)
)
)
)
),
// My other form elements
I would like to add some text similar to 'setValue' but inside HtmlTag instead of a html property of the HtmlTag.
The result I get,
<div id="DescCharsRemaining" setvalue="2000 Characters Remaining"></div>
The result i want,
<div id="DescCharsRemaining">2000 Characters Remaining</div>
By using Zend_Form_Element_Text you can insert your own html/text inside the form quite simply by doing the following.
$text = new Zend_Form_Element_Text('descCharsRemaining');
$text->setValue("<p>2000 Characters Remaining</p>")
->helper = 'formNote';
Then inserting the object into the addElements array
$this->addElements(array(
// My other form elements
$text,
// My other form elements
UPDATE: I have discovered a more simplified way to do this by just including the following directly into the array
array(
'note',
'desc',
array(
'value' => '<p>2000 Characters Remaining</p>'
),
),
With the result being,
<dd id="descCharsRemaining-element">
<p>2000 Characters Remaining</p>
</dd>
A div isn't a form element, I don't see how it will accept a value. To do this you can create a new zend_form element by extending zend_form_element, create a custom decorator and control how the element is rendered or write a view script to replace the decorator.

CakePHP form appending div and label automatically

I am using Cakephp 2.2.3 version. When I create form using Cake form helpers, it automatically appends a div and label to the input type field. How to avoid it?
Following is the code:
<?php echo $this->Form->input('username', array('id' => 'username', 'class' => 'login-inp', 'type' => 'text')); ?>
You can use options array of input to avoid form appending div and label automatically. Set div and label of options array to false.
echo $this->Form->input('username',
array('id' => 'username', 'class' => 'login-inp',
'div' => false, 'label' => false
)
);
That's what FormHelper::input() is supposed to do. If you don't want the label and wrapping div just use the functions to generate specific input elements only like FormHelper::text(), FormHelper::select(), FormHelper::radio(), etc.

cakePHP assign selected values from 2 arrays using the html input helper

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
);

Categories