Populate dropdown in ATK4
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?
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.
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');
精彩评论