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.
Related
With CakePHP 3, displayValue option allows you to change the field which will be displayed in your view, for example a name instead of an id.
find('list', [
'keyField' => 'no_user',
'order' => ['Users.name' => 'asc'],
'valueField' => ['name']
]);
In my example above, I want to display name and first name, but I can't find how to properly use valueField to display multiple fields.
I tried :
'valueField' => ['name', 'first_name']
The output is name;first_name in my view.
The book only gives an example with a single field.
How can I format it to have name first_name ?
Thanks in advance
Take a look at virtual properties: http://book.cakephp.org/3.0/en/orm/entities.html#creating-virtual-properties.
Looking at http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs could say you can use virtual properties.
So, I have models associated via HABTM. In one view, I'm using checkboxes to save associations. I can save data no problem. But what I can't determine is how cleanly check boxes of pre-existing associations, say, when I go onto an Edit page and want to edit associations.
In this case, I have a User model and a Survey model.
Here's the code for my view:
foreach ($users as $user) {
echo $this->Form->input('User.User.', array('type' => 'checkbox', 'value' => $user['User']['id'], 'hiddenField' => false, 'label' => false )) . ' ' . $user['User']['username'];
}
There must be a way to simply mark boxes as selected where appropriate.
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'.
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'
));