How to add attribute to an input in Cakephp - cakephp

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'=>""
));

Related

CakePHP 2 output checkbox array inside loop

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>

Multiple file upload in custom form with managed_file

I'm building a custom settings form in Drupal 7, with an image upload field. This image field should allow multiple uploads.
After some research I found out you can do this with managed_file and '#attributes' => array('multiple' => 'multiple'). However this doesn't seem to do anything.
This is the code I currently have:
$form['frontpage_banner_images'] = array(
'#type' => 'managed_file',
'#title' => t('Frontpage Images'),
'#name' => 'files[]',
'#attributes' => array(
'multiple' => 'multiple',
'class' => 'testclass',
),
'#upload_location' => 'public://homepage-banners/',
'#default_value' => variable_get('frontpage_banner_images'),
);
With this as result:
<div class="form-item form-type-managed-file form-item-files-">
<label for="edit-frontpage-banner-images-upload">Frontpage Images</label>
<div id="edit-frontpage-banner-images-upload" class="testclass form-managed-file">
<input type="file" id="edit-frontpage-banner-images-upload" name="files[frontpage_banner_images]" size="22" class="form-file">
<input type="submit" id="edit-frontpage-banner-images-upload-button" name="frontpage_banner_images_upload_button" value="Upload" class="form-submit ajax-processed">
<input type="hidden" name="frontpage_banner_images[fid]" value="0">
</div>
</div>
As you can see, the testclass from my #attributes is being applied on the wrapping div and not on the file input. So the multiple attribute isn't doing anything.
This is what I'm trying to achieve (Photoshopped):
Any help on how to achieve this is appreciated.
The multiple option does not seem to be supported. Then again you are using it under the #attributes. This would cause IMO the html element to have an atttribute multiple, but I don't see anything in /modules/file/file.js to pick up on that, so that's presumably why it's not doing anything.
You're probably better off using plupload, as suggested here.

How to format the name of the checkbox while send by a GET cakephp

I would like to know if it's possible to format the name of the checkbox when I try to send it to my other page via a GET method.
Actually I've got a multiple checkboxes that generate something like:
<div class="checkbox"><input type="checkbox" name="test[]" value="1" id="ResearchTest1" /><label for="ResearchTest1">First Test</label></div>
<div class="checkbox"><input type="checkbox" name="test[]" value="2" id="ResearchTest2" /><label for="ResearchTest2">Second Test</label></div>
<div class="checkbox"><input type="checkbox" name="test[]" value="3" id="ResearchTest3" /><label for="ResearchTest3">Third Test</label></div>
And when I send my form, my URL looks like:
research%3D%26test%3D%26test%5B%5D%3D1%26test%5B%5D%3D2%26test%5B%5D%3D3
Which is :
research=&test=&test[]=1&test[]=2&test[]=3
And what I would like, will be:
research=&test1=1&test2=2&test3=3
Or
research=&test=1&test=2&test=3
Or, much better:
research=&test=1,2,3
Any ideas ?
That is how checkboxes work, if you want test1=1&test2=2&test3=3 then you should name each checkbox individually.
echo $this->Form->checkbox('foo', array('name' => 'test1'));
echo $this->Form->checkbox('foo', array('name' => 'test2'));
echo $this->Form->checkbox('foo', array('name' => 'test3'));
This will make your processing much harder.
You can do research=&test=1,2,3 with JS join(). Why you want to make this difficult for yourself I dont know. You can easily get that same format in the controller doint the GET with implode(',', $theData)
I would recommend using standards for submitting your form and process the data later.
You can define the name of the input using the FormHelper:
echo $this->Form->checkbox('yourInputId', array('name' => 'yourInputName'));
The problem is that I use a multiple checkboxes:
$this->Form->input('tests', array('type' => 'select', 'multiple' => 'checkbox', 'options' => $options));
So I can't rename each checkbox individually.
Maybe I should not use cakephp for this, but try to write my own checkboxes directly in html?

Chaging the label position for an input at CakePHP 2.2

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

Translating html input into cake php input convention

I am having an issue with my submit button and I think its because I just left it in the regular format echo $this->Form->end('Submit') but actually I want to translate these properties into CakePHP <input type="submit" value="Submit" name="submitrating" class="button"/> .
How can I create those properties on the submit button in CakePHP html helper form?, I am switching from a conventional php website to cake php.
Thank you
You can add attributes by passing them as an array.
$this->Form->end(
array(
'label' => 'Submit',
'name' => 'submitrating',
'class' => 'button'
)
);

Resources