The SQL type of myField is boolean ('t' or 'f'). I need "MyLabel" but the code below not works, show only a checkbox (is ok), not the label.
echo $this->Form->checkbox('myField', array('value'=>'t', 'label'=>'MyLabel') );
try
echo $this->Form->input('myField', array('type' => 'checkbox, 'value'=>'t', 'label'=>'MyLabel') );
Related
I have two tables store and users.They have one to one relation.After bake store I get user list in a select box.I want to make all selection to check box because farther I want to select multiple users for one store.Now I just need to convert this select box to check box.
I have tried
<?php
echo $this->Form->input('mad_stores_id');
?>
To
<?php
echo $this->Form->checkbox('mad_stores_id');
?>
But this is giving me only one check box.I need to display all option which have given in selection box.
Here is the controller find methods
$users = $this->UserStoreSelection->Users->find('list',array('fields' => array('id','username')));
How can I show all select option in check box ?
If you're relationship is base on One to One, then you shouldn't allow end users to select multiple users for store. That would be a hasMany relationship.
Anyway, here you go
<?php echo $this->Form->input('mad_stores_id', array(
'multiple' => 'multiple')); ?>
Edit: if its multiple checkboxed yo want, then its the following:
<?php echo $this->Form->input('mad_stores_id', array(
'multiple' => 'checkbox')); ?>
I have following form field in a registration form in cakephp. I want to make it 'hard-coded', so user can't edit it
echo $form->input('name', array('label' => __('Name *', true)));
Then don't add it to the form.
Those fields should be added in the controller (or even beforeValidate/beforeSave model layer) then right before saving:
if ($this->request->is('post')) {
$this->User->create();
// add the content before passing it on to the model
$this->request->data['User']['status'] = 1;
if ($this->User->save($this->request->data)) {
...
}
}
See "default values - hidden" here.
You can set the readonly property:
echo $form->input('name', array('label' => __('Name *', true), 'readonly' => true));
However, this only affects the UI, and so you still have to apply mark's answer to ensure the value doesn't get changed by the user.
Two options:
hard code the value before the save
use white list
If you want the field to be a read-only, from the moment it is set. use white list. this way - it doesn't matter if the user will submit the field or not. cake won't save it.
$white_list = array('title', 'category');
$this->Model->save($data,$validate,$white_list);
The other solution is as mark coded it:
$this->request->data['User']['status'] = 1;
if ($this->User->save($this->request->data)) {
...
}
Any solution should mix a UI indication that the field will not be changed. tho a good UX will not allow it in the first-place.
I would like to make a configuration page like this for my CMS
http://img4.hostingpics.net/pics/72087272ok.png
I want this page (admin/settings/index) gets me the various settings (ID 1 to 21) came to my table and in case of change, I can, in this form, do an update
After 3 days of not especially fruitful research I found something. I put in my SettingsController:
admin_index public function () {
if (!empty($this->data)) {
$this->Setting->saveAll($this->request->data['Setting']);
} else {
$this->request->data['Setting'] = Set::combine($this->Setting->find('all'),
'{n}. Setting.id', '{n}. Setting');
}
}
and my view:
<?php
echo $this->Form->create('Setting', array ('class' => 'form-horizontal'));
foreach($this->request->data['Setting'] as $key => $value) {
echo $this->Form->input('Setting.' . $key . '.pair');
echo $this->Form->input('Setting.' . $key . '.id');
}
echo $this->Form->end(array('class' => 'btn btn-primary', 'label' => 'Save',
'value' => 'Update!', 'div' => array ('class' => 'form-actions')));
?>
he is recovering well all my information, I can even update. The trouble is that I do not really know how I can do to get the same result as my first screenshot. He puts everything in the textarea while in some cases I want the checkbox and / or drop-down.
please help me or explain to me how to make a page that retrieves configuration information from my table and allows me to edit without use an address like admin/settings/edit/ID
My table settings is something like this here
http://img4.hostingpics.net/pics/724403settings.png
If you are asking about how to format the output to produce a "settings" page, you will need to do it manually or add some kind of metadata to your table schema, such as "field_type".
Now Cake will automatically generate a checkbox for a boolean or tiny int, for example, but as you have varchars, ints, texts and so on, this won't work for you (pair is presumably varchar or text to allow for the different possible values.
You cannot really automagically generate the outputs you want without telling Cake.
One option would be to add a field_type to the table, so:
key value field
site_name My Site input
site_online 1 checkbox
meta_desc [some text] textarea
and something like
foreach($settings as $setting) {
echo $this->Form->{$setting['field']}($setting['key'],
array('value' => $setting['value']));
}
might do what you are after.
Or if($key=='site_name') // output a textbox
but this is not exactly ideal.
I'm pretty sure I don't have this problem in Cake 1.3, but:
I have a form input based on an is_live db field (containing 1 or 0 as its value).
The following creates a correctly populated checkbox:
echo $this->Form->input('is_live', array('label'=>'Status'));
However, the following does not seem to create a correctly populated dropdown (the first option is always selected, even though selecting an item and submitted the form does update correctly):
echo $this->Form->input('is_live', array(
'label'=>'Status', 'type'=>'select' , 'options'=>array(1=>'Live', 0=>'Pending')
));
Is there anything simple I can do to make the dropdown populate based on the value of is_live in CakePHP 2.0? Or is there a workaround?
I had the same issue with using 1 and 0 before.
My solution is to use the following
$options = array(1=> 'Live', 0=>'Pending');
echo $this->Form->input('YourModel.is_live',
array(
'options' => $options,
'label' => 'Status',
'selected' => intval($defaultValue), // make sure you set a default value
)
);
Can you change the content length of that field? If you can, change it to 2. This will get around the problem.
ALTER TABLE `your_table` CHANGE `is_live` `is_live` TINYINT(2) NULL DEFAULT NULL;
I am using the cakephp form helper. Basically I want to output a select box with 2 options, public and private. I want private to be selected by default. Does anyone know how to get the private option selected by default? This is what I have so far:
echo $this->Form->input('profile', array(
'type' => 'select',
'options' => array('public' => 'public', 'private' => 'private'),
'selected' => 'private'
));
The private value isn't selected though
Thanks
I've tried your code and it works nice. "Private" appears selected by default.
If I understand you correctly, 'public'/'private' maps to a tinyint(1) in the database and therefore shows a checkbox by default. That is why you had to include 'type' => 'select'.
If that is the case, you can specify the index, as in 'selected' => '1'.