tags:

views:

281

answers:

1

Desc Model belongsTo Prod Model. I want that all Prod.Name will appear as checkboxes when adding a new desc, so that user will just click a Prod.Name when adding a new description for it. Like:

<?php
echo $form->create('Desc');
echo $form->checkBox(Prod.Name); // assuming this is the correct code.
echo $form->textArea('Desc.content');
echo $form->end('Save');
?>

I'm still not familiar with this framework, still messing with it.

Thanks!

So far this is what I did:

<select name='data[Desc][prod_id]' id='DescriptionProdId'>
<?php echo $form->create('Desc'); ?>
<?php foreach($opps as $opp): ?>
<option value="<?php $opp["Prod"]["id"] ?>">
<?php echo $opp["Prod"]["name"]; ?>
</option>
<?php endforeach; ?>
</select>
A: 

Instead of creating the element manually, you should use the FormHelper.

In your view:

<?php
echo $form->input('prod_id', array('options' => $opps));
?>

Cakephp will make the select input, using the $opps records as the options. You can also set other options other than the 'options' option. Check out:

http://book.cakephp.org/view/189/Automagic-Form-Elements

If you specify the view variable as prods in your controller action, then you do not need to specify the options key of the $options array. In the controller action:

$this->set('prods', $this->find('all'));
adam
will it display all the prod name in the database?
jun
Yes. You should never write custom form elements with cakephp. Learn how to use the FormHelper.
adam
In your $form->checkbox() call, the argument is missing the string quotations around it. Also, you should always use $form->input(), and cakephp will output the appropriate form type based on the database schema. If you need a different input type, then use this option: $form->input('Model.field', array('type' => 'textarea'));
adam
`$this->set('prods', $this->Desc->Prod->find('list'));` .. if you name your variable plural like this (`$prods`), cakephp will automatically populate the select box, so you don't have to provide the `'options' => $opps` part.
deizel