2
function init(){
    parent::init();
    $f = $this->add('Form');
    $f->addField('dropdown', 'Label:')->setModel('User');
}

So this code will output a dropdown list populated by the values in the table asosiated with the model User, but the values will be does of the name field in the model.

Is there a way to use another field of the model to populate this?

Jmsegrev
  • 251
  • 1
  • 2
  • 10

2 Answers2

2

No direct way. First, are you sure you don't have to use 'reference' type instead of dropdown?

Secondly, free is how:

class Model_User_BySurname extends Model_User {
    public function getListFields(){
        return array('id'=>'id','surname'=>'name');
    }
}

then further:

$form->addField('reference','Label')->setModel('User_BySurname');

Of course you can make this field re-defineable in your models, by creating some sort of "setNameField('surname')" function and hidden property used in getListFields.

romaninsh
  • 10,606
  • 4
  • 50
  • 70
  • Thanks for the fast and clear answer, this is great, im loving ATK4, hope the API to get better documented, i think this framework will be one of the tops if this happens. – Jmsegrev Oct 04 '11 at 00:33
  • Ok so i couldn't find any information about this reference field, from what i've seen works the same way as the dropdown, is there any particularity? – Jmsegrev Oct 04 '11 at 00:40
  • @Juanma Thanks! "reference" was initially developed to work with models in particular. if dropdown->setModel does not work, use reference. – romaninsh Oct 04 '11 at 02:41
0

This has changed and took me some time to figure out how to now do this.

class Model_MyModel extends SQL_Model {
  public function init() {
    parent::init();
    $this->addField('titleField');
  }

  public function getTitleField() {
    return 'titleField';
  }
}

$form->addField('dropdown', 'Label')->setModel('MyModel');
Geoffrey
  • 10,843
  • 3
  • 33
  • 46