Note that there are some explanatory texts on larger screens.

plurals
  1. POcake populate a dropdown from model
    text
    copied!<p>I am trying to populate a dropdpwn with some options. I am trying to have a dropdown with name title and country list for my input form.</p> <p>For example:</p> <p>Titles I need 'Mr', 'Mrs', 'Miss'</p> <p>I have tried to ways:</p> <p>Model</p> <pre><code>// Built a list of search options (unless you have this list somewhere else) public function __construct($id = false, $table = null, $ds = null) { $this-&gt;titles = array( 0 =&gt; __('Mr', true), 1 =&gt; __('Mrs', true), 2 =&gt; __('Miss', true)); parent::__construct($id, $table, $ds); } </code></pre> <p>Controller</p> <pre><code>public function add() { if ($this-&gt;request-&gt;is('post')) { $this-&gt;Member-&gt;create(); if ($this-&gt;Member-&gt;save($this-&gt;request-&gt;data)) { $this-&gt;Session-&gt;setFlash(__('The member has been saved')); $this-&gt;redirect(array('action' =&gt; 'index')); } else { $this-&gt;Session-&gt;setFlash(__('The member could not be saved. Please, try again.')); } } } </code></pre> <p>View</p> <pre><code>&lt;?php echo $this-&gt;Form-&gt;input('title',array('class' =&gt; 'span10', 'options' =&gt; $titles )); ?&gt; </code></pre> <p>I get the error Undefined variable: titles</p> <p>I have also tried in the model</p> <pre><code>public $validate = array( 'member_no' =&gt; array( 'notempty' =&gt; array( 'rule' =&gt; array('notempty'), //'message' =&gt; 'Your custom message here', //'allowEmpty' =&gt; false, //'required' =&gt; false, //'last' =&gt; false, // Stop validation after this rule //'on' =&gt; 'create', // Limit validation to 'create' or 'update' operations ), ), 'title' =&gt; array( 'titlesValid' =&gt; array( 'rule' =&gt; array('multiple', array( 'in' =&gt; array('Mr', 'Mrs', 'Miss'), 'min' =&gt; 1 )), </code></pre> <p><em>What I am missing and what would be the best solution for a longer list such as countries, it is only on form so i didnt think I would nees a title and countries table and link ids?</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload