Keith Power
Keith Power

Reputation: 14141

cake populate a dropdown from model

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.

For example:

Titles I need 'Mr', 'Mrs', 'Miss'

I have tried to ways:

Model

// Built a list of search options (unless you have this list somewhere else)
public function __construct($id = false, $table = null, $ds = null) {
$this->titles = array(
             0 => __('Mr', true),
             1 => __('Mrs', true),
            2 => __('Miss', true));
 parent::__construct($id, $table, $ds);
 }

Controller

public function add() {
    if ($this->request->is('post')) {
        $this->Member->create();
        if ($this->Member->save($this->request->data)) {
            $this->Session->setFlash(__('The member has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The member could not be saved. Please, try again.'));
        }
    }
}

View

<?php echo $this->Form->input('title',array('class' => 'span10', 'options' => $titles   )); ?>

I get the error Undefined variable: titles

I have also tried in the model

public $validate = array(
    'member_no' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'title' => array(
        'titlesValid' => array(
            'rule' => array('multiple', array(
                'in'  => array('Mr', 'Mrs', 'Miss'),
                'min' => 1
            )),

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?

Upvotes: 0

Views: 201

Answers (1)

Nunser
Nunser

Reputation: 4522

I find really weird that you have to create a variable in a construct of a model for a dropdown. Specially if you're only going to use it once.

But first things first. You're getting that error because you haven't set the variable to the view. In your controller, there needs to be something like this:

public function add() {
    $this->set('titles', $your_titles_array);
    if ($this->request->is('post')) {

    //etc

Now, please please get the dropdown array away from there. There are two possible nicer places to put that. I'm not going to get picky and say you need to have this values as a table in you database. If you say it's just for one place and you want to have it hardcode, so be it.

One option is to put it in one model, like

class YourModel extends AppModel {
    //all your other definitions

    public function getTitles() {
        return array(
         0 => __('Mr', true),
         1 => __('Mrs', true),
        2 => __('Miss', true));
    }
}

And in the controller do

    public function add() {
    $this->set('titles', $this->YourModel->getTitles());
    if ($this->request->is('post')) {

    //etc

But, I don't think you have an appropriate model to add that function. Where did you plan to add it? User model maybe? It can't be like Post model, for example, that wouldn't make any sense... So, give it a thought, if there's a logical place in a model you can put that function, then go ahead.

Otherwise, and if it's just for one form in one place just that one time, why not hardcode it to the view or the controller?

For larger lists, my recommendation is to do it in a model like I showed (but I insist, put it in the model where is logical to have it). Well, I rather have a table and reference the values, but it's not mandatory (though give it a thought, is really not that much work, is it? for larger lists, I mean).

Upvotes: 3

Related Questions