cakephp form helper select box with selected value - cakephp

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'.

Related

Auto Increment number on SuiteCRM Accounts

I tried several answers from suitecrm forum And here. And I can't implement them to the present version of Suite CRM.
Here is the one I tried last and stuck for further clarification.
add a new file in custom/Extension/modules/yourmodule/Ext/Vardefs e.g. autoincrement.php with the following :
<?php
$dictionary['YOUR_MODULE']['fields']['NAME_OF_AUTO_INC_FIELD'] = array(
'name' => 'NAME_OF_AUTO_INC_FIELD',
'vname' => 'LBL_LABEL_OF_AUTO_INC_FIELD',
'type' => 'int',
'readonly' => true,
'len' => 11,
'auto_increment' => true,
'disable_num_format' => true,
);
?>
and also add unique index for the field in that file
<?php
$dictionary['YOUR_MODULE']['indices']['NAME_FOR_INDEX'] = array(
'name' => 'NAME_FOR_INDEX',
'type' => 'unique',
'fields' => array('NAME_OF_AUTO_INC_FIELD'),
);
?>
Run a Quick Rebuild and Repair in Admin -> Repair and execute the changes.
after that it shows an empty text box. There it iterates automatically, when new account saved. But I want to show the next auto increment number here in this Accounts page itself.
Instead of default value, I want to show the next auto value in the new Account form.
This isn't out the box behaviour - you'll need to add some customisations to do this.
I would create a new vardef auto_inc_preview which is a function type field. This can then be used to grab the largest number from the DB and display this + 1.
A possible issue with this would be that the number a user sees may not be the id that gets generated - for example if two or more people create an account at the same time.

Option usage configureListFields in SonataAdminBundle

unfortunately documentation doesn't cover how to use options available in configureListFields ListMapper when you add fields to the list.
This is my basic code
$listMapper
->add('myField', null, array(
'label' => LabelHelper::LABEL_MY_FIELD,
'code' => // what should I put here ... $this->methodName() is not working
))
I want to use 'code' option (docs - section 7.2.1), because I would like to customize just one filed final display. I don't want to rewrite the row template.
As stated in the code section I've tried simple method that returns string, but nothing happened in the list view (I've cleared cache etc.).
Answer is simple. You just put method name without brackets
$listMapper
->add('myField', null, array(
'label' => LabelHelper::LABEL_MY_FIELD,
'code' => 'methodName'
))
Method should be stored in corresponding Entity class

Having Trouble Generating A Simple Select Dropdrown In CakePHP 2.0

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;

Change individual labels with automagic form helper on a 'multiple' => 'checkbox'

I have the following line in my add/edit Course views:
echo $this->Form->input(
'Competency',
array(
'label' => 'Which competencies does this course address?',
'multiple' => 'checkbox'
)
);
(Modelled as Course hasMany Competencies)
I'd like to customise the label that gets output for each competency but can't seem to find a way of doing so - the 'label' field seems to work as a group heading rather than changing the label for the individual checkboxes.
(What I'm ideally after is rather than just displaying Competency.name I can display Competency.name plus the Competency.code as the label)
NB I thought about changing the displayField but that would change it everywhere and it's only here I'd like to be different.
Create a virtual field [details]:
//in your Competency model
var $virtualFields = array(
'name_code' => 'CONCAT(Competency.name, " ", Competency.code)'
);
Then in your controller, before retrieving the data, set your displayField to your just-created virtual field:
//in your controller prior to the find
$this->Competency->displayField = 'name_code';
Since you're setting displayField in the controller, it doesn't set it permanently, so no need to set it back, but if you're doing more finds immediately after this, you can always set back to name if you want.

CakePHP AutoComplete Question

I am working on a book review application and I am using autoComplete to search for titles in when creating a review. The review model has an associated book_id field and the relationship is setup as the review hasOne book and a book hasMany reviews.
I am trying to pass the Book.id (into the book_id field), but I want to display Book.name for the user to select from. With the default setup (accomplished via CakePHP's tutorial), I can only pass Book.name. Is it possible to display the name and pass the id?
Also, I am passing it via the following code in the create() action of the review controller:
$this->data['Review']['book_id'] = $this->data['Book']['id'];
Is that the proper way to do it in CakePHP? I know in Ruby on Rails, it is automatic, but I can't seem to make it work automagically in CakePHP. Finally, I am not using the generator because it is not available in my shared hosting environment... so if this is the wrong way, what do I need other than associates in my models to make it happen automatically?
Thanks for the help and I promise this is my question for awhile...
UPDATE- I tried the following, but it is not working. Any ideas why?
function autoComplete() {
$this->set('books', $this->Book->find('all', array(
'conditions' => array(
'Book.name LIKE' => $this->data['Book']['name'].'%'
),
'fields' => array('id','name')
)));
$this->layout = 'ajax';
}
The problem is that when I use the code above in the controller, the form submits, but it doesn't save the record... No errors are also thrown, which is weird.
UPDATE2:
I have determine that the reason this isn't working is because the array types are different and you can't change the array type with the autoComplete helper. As a workaround, I tried the follow, but it isn't working. Can anyone offer guidance why?
function create() {
if($this->Review->create($this->data) && $this->Review->validates()) {
$this->data['Review']['user_id'] = $this->Session->read('Auth.User.id');
$this->Book->find('first', array('fields' => array('Book.id'), 'conditions' => array('Book.name' => $this->data['Book']['name'])));
$this->data['Review']['book_id'] = $this->Book->id;
$this->Review->save($this->data);
$this->redirect(array('action' => 'index'));
} else {
$errors = $this->Review->invalidFields();
}
}
FINAL UPDATE:
Ok, I found that the helper only takes the find(all) type or array and that the "id" field wasn't passing because it only applied to the autoComplete's LI list being generated. So, I used the observeField to obtain the information and then do a database lookup and tried to create a hidden field on the fly with the ID, but that didn't work. Finally, the observeField would only take the characters that I put in instead of what I clicked, due to an apparent Scriptaculous limitation. So, I ended up going to a dropdown box solution for now and may eventually look into something else. Thanks for all of the help anyway!
First of all, $this->data will only contain ['Book']['id'] if the field exists in the form (even if it's hidden).
To select something by name and return the id, use the list variant of the find method, viz:
$selectList = $this->Book->find('list', array(
'fields' => array(
'id',
'name'
)));
$this->set('selectList', $selectList);
In the view, you can now use $selectList for the options in the select element:
echo $form->input('Book.id', array('type' => 'hidden'));
echo $form->input('template_id', array(
'options' => $selectList,
'type' => 'select'
));

Resources