So Here is how the select boxes are being created:
echo $this->Form->select('weekdays', array(
'1'=>__('Manday'),
'2'=>__('Tuesday'),
'3'=>__('Wednesday'),
'4'=>__('Thursday'),
'5'=>__('Friday'),
'6'=>__('Satruday'),
'7'=>__('Sunday')),
array('multiple'=>'checkbox', 'class'=>'checkbox2'));
which outputs 7 checbox with the following HTML markup:
<div class="checkbox2">
<input type="checkbox" name="data[Event][weekdays][]" value="1" id="EventWeekdays1" />
<label for="EventWeekdays1">Monday</label>
</div>
But the expected output that is needed is this:
<div class="checkbox2">
<input type="checkbox"
name="data[Event][EventWeekdays1]"
id="EventWeekdays1" class="css-checkbox" value="1" />
<label for="EventWeekdays1" class="css-label">Mandag</label>
</div>
I have been through documentation but cannot seem to find an option to add clases to both input and label attributes when using Form->select
Any help or guidance is much appreciated.
Using just FormHelper-approved things:
$options = array(
'1'=>__('Manday'),
'2'=>__('Tuesday'),
'3'=>__('Wednesday'),
'4'=>__('Thursday'),
'5'=>__('Friday'),
'6'=>__('Satruday'),
'7'=>__('Sunday'));
$this->Form->input('weekdays', array(
'type' => 'select',
'div' => array('class'=>'checkbox2'),
'multiple'=>'checkbox',
'class'=>'css-checkbox',
'options' => $options,
'label' => array('class'=>'css-label')));
Docs here
I think after reading your question you would be better off building the markup yourself and loop trough your $options on the checkboxes you need, so:
$options = array(
'1'=>__('Manday'),
'2'=>__('Tuesday'),
'3'=>__('Wednesday'),
'4'=>__('Thursday'),
'5'=>__('Friday'),
'6'=>__('Satruday'),
'7'=>__('Sunday'));
<div class="checkbox2">
<?php foreach($options as $k => $v): ?>
<input type="checkbox"
name="data[Event][EventWeekdays<?php echo $k ?>]"
id="EventWeekdays<?php echo $k ?>" class="css-checkbox" value="<?php echo $v ?>" />
<label for="EventWeekdays<?php echo $k ?>" class="css-label">Mandag</label>
<?php endforeach; ?>
</div>
Not the better way to do it, but it works and can be a solution to your problem. Hope it helps!
Related
I've read CakePHP multiple checkbox array HTML the right way but getting strange results. I have a list of tags (from a Model called Tag). I want to loop through these and output checkboxes for them in a View.
So I have obtained the Tag data in my Controller with:
$tags = $this->Tag->find('list', ['order' => ['name' => 'ASC']]);
$this->set('tags',$tags);
When I loop through it in my View I am trying to output the checkboxes in between Bootstrap markup:
<?php echo $this->Form->create('GroupTag'); ?>
<?php foreach ($tags as $tag_id => $tag): ?>
<div class="checkbox">
<label>
<?php echo $this->Form->checkbox('tag_id[]', array( 'value'=> $tag_id)); ?>
<?php echo $tag; ?>
</label>
</div>
<?php endforeach; ?>
I copied the syntax for tag_id[] from the post I linked to.
But when I inspect the markup it's producing the following as the name attribute for each <input type="checkbox">:
data[GroupTag][tag_id[]]
Should this not be
data[GroupTag][tag_id][]
?
The idea is that I have multiple checkboxes with a name attribute tag_id[] and then in the Controller I can loop through what has been checked.
Please can someone advise on this as I can't get it working and have looked into examples provided on here/docs.
Try this
$this->Form->checkbox('tag_id.', array( 'value'=> $tag_id))
You can also do this:
$this->Form->input('GroupTag.tag_id', [
'type' => 'select',
'multiple' => 'checkbox',
'options' => $tag_id
]);
FormHelper::select(string $fieldName, array $options, array $attributes)
Example:
$options = array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
);
echo $this->Form->select('Model.field', $options, array(
'multiple' => 'checkbox'
));
Output:
<div class="input select">
<label for="ModelField">Field</label>
<input name="data[Model][field]" value="" id="ModelField"
type="hidden">
<div class="checkbox">
<input name="data[Model][field][]" value="Value 1"
id="ModelField1" type="checkbox">
<label for="ModelField1">Label 1</label>
</div>
<div class="checkbox">
<input name="data[Model][field][]" value="Value 2"
id="ModelField2" type="checkbox">
<label for="ModelField2">Label 2</label>
</div>
</div>
I'm developing an app in cakePHP and I'd like to use Foundation Tooltips into it.
I need to add data-tooltip to the input in order to make it work, but I don't know how to do it with the cakePHP form helper.
Right now I'm using:
<?php echo $this->Form->input('title', (array( 'label' => __('title'),
'class' => 'has-tip',
'title' => __('Tooltip for initiative title'),
)));
And is returning this:
<input name="data[Initiative][title]" class="has-tip" title="Tooltip for initiative title" maxlength="255" type="text" id="InitiativeTitle" required="required">
When I need this:
<input data-tooltip name="data[Initiative][title]" class="has-tip" title=...>
I've tried adding 'data-tooltip'to the input array with no luck, but I'm sure it has to be an easy way of doing it.
Thanks in advance and sorry for my English.
<?php echo $this->Form->input('title', array( 'label' => __('title'),
'class' => 'has-tip',
'title' => __('Tooltip for initiative title'),
'data-tooltip'=>""
));
CakePHP usually place labels before the input, so doing this:
echo $this->Form->input('subject');
We obtain this:
<div class="input text required">
<label for="TicketSubject">Subject</label>
<input name="data[Ticket][subject]" maxlength="255" type="text" id="TicketSubject">
</div>
Is there any way to place the label after the input to obtain this?
<div class="input text required">
<input name="data[Ticket][subject]" maxlength="255" type="text" id="TicketSubject">
<label for="TicketSubject">Subject</label>
</div>
Thanks.
The proper way is using the the 'format' option.
$this->Form->input('subject', array(
'format' => array('before', 'input', 'between', 'label', 'after', 'error')
));
Didn't anyone read the API :)
You can try this:
echo $this->Form->input('subject', array('label' => false, 'after' => $this->Form->label('Subject:')));
You can do like this also -
echo $this->Form->input('subject', array('label' => false, 'after' => '<label for="subject">Subject</label>'));
I can get Cake to output a submit button using the following PHP:
<?php echo $this->Form->end(__('Submit')); ?>
which outputs this HTML:
<input type="submit" value="Submit">
but I want to use a specific input class to get the following:
<input type="submit" value="Submit" class="some class">
is this possible?
Thank you :).
Yes it's possible :
<?php
echo $this->Form->submit(__('Submit',true), array('class'=>'some class'));
echo $this->Form->end();
?>
Which is documented here.
This should do the trick:
<?php echo $this->Form->end(array('label' => __('Submit', true), 'class' => 'some class')); ?>
I have a zend_form with a checkbox:
$horz = new Zend_Form_Element_Checkbox('horizontal');
$horz->setLabel('Display horizontally?');
$horz->setDecorators(array(array('ViewScript', array('viewScript' => 'partials/forms/checkbox.phtml'))));
My custom viewScript checkbox.phtml looks like this:
<?php
$name = $this->element->getName();
$attrs = $this->element->getAttribs();
$options = $attrs['options'];
?>
<dt id="<?= $name ?>-element">
<input type="hidden" value="<?= $options['uncheckedValue'] ?>" name="<?= $name ?>" />
<label>
<input type="checkbox" class="checkbox" value="<?= $this->element->getValue() ?>" id="<?= $this->element->getId() ?>" name="<?= $name ?>">
<?= $this->element->getLabel(); ?>
</label>
</dt>
It renders beautifully, but when I populate the form from a database record:
$record = array('horizontal'=>1);
$form->populate($record);
The checkbox is not checked. Is there something I need to set in my viewScript to allow it to populate?
You have to check the box in your html view script after the value is populated.
<input type="checkbox" class="checkbox" value="<?= $this->element->getValue() ?>" id="<?= $this->element->getId() ?>" name="<?= $name ?>" <?php echo $this->element->isChecked() ? " checked=\"checked\"" : "" ?> />