CakePHP form appending div and label automatically - php

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.

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: FormHelper not saving data from two inputs with same name

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

how to use Yii Eselect2 to save more data?

I use Eselect2 Yii extension to give users multiple choice but only last choice is submitted via POST. Why?
This is my html, in which I also tried to manage all choices with array but without success
<pre>echo $form->labelEx($model,'city_id');
$this->widget('ext.select2.ESelect2', array(
'name' => 'Form[field]',
'data' => City::model()->getCitie`enter code here`s(),
'options' => array('width' => '30%','allowClear'=>true),
'htmlOptions'=>array(
'options'=>array(''=>array('value'=>null,'selected'=>null, 'name'=>'field'),),
'multiple'=>'multiple',
)
));
</pre>
I tried to specify 'name' field as single field and as an array but I have the same problem: only last value is sended.
You should simply use an array :
Instead of
'name' => 'Form[field]',
You should try :
'name' => 'Form[field][]',

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.

Why is CakePHP Form helper grouping my form radio options?

echo $this->Form->create('AmazonMatches', array('action' => 'selectMatches'));
echo $this->Form->input('option_id', array('options' => $allAmazonMatches, 'type' => 'radio'));
echo $this->Form->end(__('Submit', true));
Now I see a box around my radio buttons with a large red text saying "Option Id".
How can i get rid of it? Sorry I am a total Cake noob.
You need to set the 'legend' option to false if you don't want to show it, or to a string if you want to customize the message:
echo $this->Form->input('option_id', array(
'options' => $allAmazonMatches,
'type' => 'radio',
'legend' => false
));
$this->Form->input
Creates one input field with the id provided. You'll have to create multiple inputs inorder to make your checkboxes work separately.
There could be better methods, but doing it something likethis will work.
foreach($allAmazonMatches as $amazonMatch)
{
$this->Form->input...
}

Categories