How to add custom id attribute in radio button cakephp 3 - cakephp

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

Related

cakephp3 and select2 how to reset the first value

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

how to pass class in checkbox using cakephp form

i am using below code:-
echo $this->Form->input('my_radio',array(
'label' =>false,
'type' => 'select',
'multiple' => 'checkbox',
'class'=>'checkbox12',
'div'=>false,
'selected' => array_keys($sub_cat_name1),
'options' => $parent_cat_detail1
));
i did false the div but still the 'checkbox12' is adding to div.
the problem is not in the class , is in the mutiple value , you should put it true not checkbox :
echo $this->Form->input('my_radio',array(
'label' =>false,
'type' => 'select',
'multiple' => true,
'class'=>'checkbox12',
'selected' => array_keys($sub_cat_name1),
'options' => $parent_cat_detail1
));

How to save multiple instances of data using one form in CakePHP

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

CakePHP 1.3 - Add extra attributes to select menu options

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')
]) ?>

MongoDB and CakePHP Model associations

I'm trying to make a Match model which includes the players lined up during that match. So each Match hasMany Players and each Player hasmany Matches.
match = {
'_id' : ObjectID('978tqwbi9873gofiu'),
'home' : 'Argentina',
'away' : 'Brazil',
'lineup-home' : [
{'name' : 'Lionel Messi',
'goals' : '2',
'timeon' : 30
},
{'name' : 'Diego Maradonna',
'goals' : '0',
'timeon' : 0
},
{'name' : 'Sergio Aguero',
'goals' : '0',
'timeon' : 0
}
]
}
How do I add these 'lineup-home' relations in my CakePHP model to work with my mongoDB? This is how my model looks like...
class Match extends AppModel {
//var $useDbConfig = 'mongo';
var $mongoSchema = array(
'home' => array('type' => 'string'),
'away' => array('type' => 'string'),
'lineup-home' => ???
);
}
Thank you.
I think that using 'lineup-home' => 'array' will do the trick..
And how about
var $mongoSchema = array(
'home' => array('type' => 'string'),
'away' => array('type' => 'string'),
'lineup-home' => array(
'name' => array('type' => 'string'),
'goals' => array('type' => 'string'),
'timeon' => array('type' => 'integer'),
)
);
You can use this schema:
var $mongoSchema = array(
'home' => array('type' => 'string'),
'away' => array('type' => 'string'),
'lineup-home' => array(
array(
'name' => array('type' => 'string'),
'goals' => array('type' => 'string'),
'timeon' => array('type' => 'integer'),
)
)
);

Resources