How can I add additional attributes to my select menu option tags? Like this:
<select class="test" name="data[Test][test]">
<option value="1" data-price="100">My Option</option>
</select>
How do I add the data-price="100" ?
I tried something like this but it didn't work:
<?php
echo $this->Form->select('test', $options, null, array(
'class' => 'test',
'options' => array(
'data-price' => 100
)
));
?>
check this out:
http://www.dereuromark.de/2012/03/01/some-new-crazy-cakephp-tricks/
"Setting additional attributes for some select options"
you can try this
echo $this->Form->input('test', array(
'options' => array(
1=>array(
'data-price' => 100,
'value' => '1',
'name' => 'My Option'
)),'class' => 'test')
);
You can do it this way:
$options = array(
...
array('name' => 'United states', 'value' => 'USA', 'title' => 'the title that you want', 'class' => 'something'),
array('name' => 'USA', 'value' => 'USA', 'title' => 'the other title that you want', 'class' => 'otherthing'),
);
echo $this->Form->input('test', array('type'=>'select', 'options'=>$options));
You have to manually build the select HTML
also you can refer How to give select tag an attribute in cake php?
In cakephp 4
look for documentation
Controller
$countries = $this->Countries->find('all')->where([
'active' => 1
]);
$options = $examples->map(function ($value, $key) {
return [
'value' => $value->id,
'text' => $value->name,
'data-flag' => $value->iso_code
];
});
In view/template
<?= $this->Form->control('country_residence_id', [
'label' => false,
'options' => $countries,
'class' => 'form-control select2-flag-search',
'data-placeholde' => __('Select Country')
]) ?>
Related
i have to make 7 radio buttons according to days but this is generating common id all the time . i want different id's for all the radios according to days.
<?php echo $this->Form->create('', array('type' => 'post', 'class' => 'form form-horizontal', 'id'=>'addDeliveryPereferenceForm', 'novalidate')); ?>
<?php echo $this->Form->control('delivery_preference[]', ['type' => 'radio', 'label' => false, 'class' => 'form-control border-primary','id'=>'mon','options' => $preferences]); ?>
<?php echo $this->Form->control('delivery_preference[]', ['type' => 'radio', 'label' => false, 'class' => 'form-control border-primary','id'=>'mon','options' => $preferences]); ?>
How does $preferences look like ? If you could modify it to something like this, it will work. You can also add other attributes to each option created here. You should also remove the id from your code to avoid conflict.
$preferences = [
['value' => 1, 'text' => 'monday', 'id' => 'mon'],
['value' => 2, 'text' => 'tuesday', 'id' => 'tue'],
['value' => 3, 'text' => 'wednesday', 'id' => 'wed'],
['value' => 4, 'text' => 'thursday', 'id' => 'thu'],
['value' => 5, 'text' => 'friday', 'id' => 'fri']
];
FormHelper from cookbook
echo $this->Form->input('product_id', array(
'label'=>false,
'type'=>'select',
'multiple'=>'checkbox',
'options'=>$product,
));
I am trying to adding 'checked=>true' in from input but failed
this is screenshoot of the form edit, the data already selected is not checked
You can tell CakePHP which checkboxes are checked by passing an array of selected values:-
$selected = []; // An array of selected values
echo $this->Form->input('product_id', array(
'label' => false,
'type' => 'select',
'multiple' => 'checkbox',
'options' => $product,
'selected' => $selected
));
If you wanted to select all the values to start with you could pass the array keys of $product to the selected option:-
$selected = array_keys($product);
echo $this->Form->input('product_id', array(
'label' => false,
'type' => 'select',
'multiple' => 'checkbox',
'options' => $product,
'selected' => $selected
));
I am using cakephp3 and select2 dropdown script.
My call to the data looks like this
$roles = $this->ParticipantsProjectsRoles->Roles->find('list', [
'keyField' => 'id',
'valueField' => 'description'
]);
within my view I call this
<?=$this->Form->input('role_id', ['options' => $roles, 'label' => false, 'class' => 'form-control select2me']);?>
The output HTML will load always the first data entry into the select.
Is there a way to have the first value always empty?
Set the empty key in the options array to true or another value, e.g. Select Role:
$this->Form->input(
'role_id', [
'options' => $roles,
'label' => false,
'class' => 'form-control select2me',
'empty' => true
]
);
I'm trying to display listbox options in Cakephp that will have additional attributes.
For example:
<option value="1" dataval-price="5">one</option>
<option value="2" dataval-price="10">two</option>
<option value="3" dataval-price="50">three</option>
Have read elsewhere that this is just a case of building the array like so:
$options = array(
2 => array('name' => 'One', 'value' => 1, 'dataval-price' => '5'),
2 => array('name' => 'Two', 'value' => 2, 'dataval-price' => '10'),
2 => array('name' => 'Three', 'value' => 3, 'dataval-price' => '50')
);
How can I return data in this format? The helper below will return the data in a format that forces the listbox to instead use optgroups.
$optionsArray = $this->TableX->find('all', array(
'fields' => array('name', 'id', 'price'),
'order' => array('name' => 'ASC')
));
You can use aliases for fields like so:
$results = $this->Model->find('all', array(
'fields' => array('name', 'id AS value', 'price AS dataval-price'),
'order' => array('name' => 'ASC')
));
Your results will appear like this however:
array(
0 => array(
'Model' => array(
'name' => 'name',
'value' => 1,
'dataval-price' => 2.00
)
),
//etc
);
This can be fixed using CakePHP's hash class:
$results = Hash::flatten($results);
$keys = str_replace('Model.', '', array_keys($results));
$results = array_combine($keys, array_values($results));
$results = Hash::expand($results);
I have built a screen where an administrator can add and edit users. I'm able to add users without issue, but when I was testing how users can be edited, I noticed that if I have more than one user, I'm only be able to edit the last user thats listed. I'm unable to edit any other user.
Here is my code:
<?php foreach ($personel as $person) { ?>
<div id="edituser<?php echo $person['Personel']['id']; ?>" class="modal" style="display:none;">
<?php
$edituserformname = "editUser" + $person['Personel']['id'];
?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Edit User - <?php echo $person['Personel']['firstname']; ?> <?php echo $person['Personel']['surname']; ?></h3>
</div>
<div class="modal-body iframed">
<?php echo $
<?php
echo $this->Form->create('Personel', array(
'class' => 'form-horizontal',
'id' => $edituserformname
));
echo $this->Form->input('id', array(
'type' => 'hidden',
'value' => $person['Personel']['id']
));
echo $this->Form->input('firstname', array(
'type' => 'text',
'label' => 'First Name',
'class' => 'span5',
'value' => $person['Personel']['firstname']
));
echo $this->Form->input('surname', array(
'type' => 'text',
'label' => 'Surname',
'class' => 'span5',
'value' => $person['Personel']['surname']
));
echo $this->Form->input('email', array(
'type' => 'text',
'label' => 'E-Mail',
'class' => 'span5',
'value' => $person['Personel']['email']
));
echo $this->Form->input('companyid', array(
'type' => 'hidden',
'value' => $company['Company']['id']
));
echo $this->Form->input('accesslevel', array(
'label' => 'Access Level',
'options' => $roles,
'empty' => 'Select Access Level',
'class' => 'span5',
'value' => $person['Personel']['accesslevel']
));
$pocval = array('1' => 'Yes', '0' => 'No');
echo $this->Form->input('poc', array(
'label' => 'Point of Contact?',
'options' => $pocval,
'value' => $person['Personel']['poc']
));
echo $this->Form->input('password', array(
'type' => 'text',
'label' => 'Password',
'class' => 'span5',
'value' => $company['Personel']['password']
));
echo $this->Form->input('telephone', array(
'type' => 'text',
'label' => 'Telephone',
'class' => 'span5',
'value' => $company['Personel']['telephone']
));
echo $this->Form->input('type', array(
'type' => 'hidden',
'value' => '0'
));
?>
</div>
<div class="modal-footer">
<?php
echo $this->Form->submit('Save & Close', array(
'type' => 'submit',
'class' => 'btn btn-primary',
'id' => 'editusermodal'
));
echo $this->Form->end();
?>
</div>
</div>
<?php } ?>
How can I fix this issue? I had an idea about using iFrame's but I'm reluctant to use this method. I'd rather be able to do it through CakePHP.
Many thanks
You don't need to use iFrame's. It doesn't work because your Form doesn't close correctly.
The $this->Form->create() and $this->Form->end() should be outside the divs.
Try the following:
<?php
echo $this->Form->create('Personel', array(
'class' => 'form-horizontal',
'id' => $edituserformname
));
?>
<div class="modal-body iframed">
<?php
echo $this->Form->input('id', array(
'type' => 'hidden',
'value' => $person['Personel']['id']
));
echo $this->Form->input('firstname', array(
'type' => 'text',
'label' => 'First Name',
'class' => 'span5',
'value' => $person['Personel']['firstname']
));
echo $this->Form->input('surname', array(
'type' => 'text',
'label' => 'Surname',
'class' => 'span5',
'value' => $person['Personel']['surname']
));
echo $this->Form->input('email', array(
'type' => 'text',
'label' => 'E-Mail',
'class' => 'span5',
'value' => $person['Personel']['email']
));
echo $this->Form->input('companyid', array(
'type' => 'hidden',
'value' => $company['Company']['id']
));
echo $this->Form->input('accesslevel', array(
'label' => 'Access Level',
'options' => $roles,
'empty' => 'Select Access Level',
'class' => 'span5',
'value' => $person['Personel']['accesslevel']
));
$pocval = array('1' => 'Yes', '0' => 'No');
echo $this->Form->input('poc', array(
'label' => 'Point of Contact?',
'options' => $pocval,
'value' => $person['Personel']['poc']
));
echo $this->Form->input('password', array(
'type' => 'text',
'label' => 'Password',
'class' => 'span5',
'value' => $company['Personel']['password']
));
echo $this->Form->input('telephone', array(
'type' => 'text',
'label' => 'Telephone',
'class' => 'span5',
'value' => $company['Personel']['telephone']
));
echo $this->Form->input('type', array(
'type' => 'hidden',
'value' => '0'
));
?>
</div>
<div class="modal-footer">
<?php
echo $this->Form->submit('Save & Close', array(
'type' => 'submit',
'class' => 'btn btn-primary',
'id' => 'editusermodal'
));
?>
</div>
<?php
echo $this->Form->end();
?>
If you want more than one record editing at a time you can use the following
$this->Form->input('ModelName.n.field_name', $options);
So to do all of them run this in a loop:
echo $this->Form->create(); // only one start
foreach($users as $k => $user) {
echo $this->Form->id(sprintf('Personel.%s.id', $k), array(
'value' => $user['Personel']['id']
));
echo $this->Form->input(sprintf('Personel.%s.field', $k), array(
'value' => $user['Personel']['field']
));
// etc
}
echo $this->Form->end(); // only one end
To save them all you can use saveAll()
$this->Personel->saveAll($this->data['Personel']);