..... 'country_id' => array( 'data_type' => 'varchar', 'label' => 'Country', 'form' => array('data_type' => 'select', 'options' => array()), 'validation' => array('required'), ), .....
static::$_properties['country_id']['form']['options'] = static::country_dropdown_list();
$form->add('country_id', '', array('options' => $ops, 'type' => 'select'));Are there any suggestions?
// set the locale options with installed locales static::$_properties['locale']['form']['options'] = \Config::get('exitecms.locales'); // set the country options static::$_properties['country']['form']['options'] = \Lang::get('countries');and this works fine. You should not use the constructor as that would mean you populate the options for every instantiation of the model. Which is not needed, the properties are static and thus global for all instances. Use the _init() method instead (altough I don't see why it would not work in the constructor).
Don't use the constructor, the constructor is only called upon object creation which is too late - at that point the properties have already been determined at the class level and it won't take another look. We have the _init() method for that reason, that's called right after the autoloader has loaded the class. Add "public static function _init() {}" and put it in there. Also you were using 'data_type' in the form setting, the data type is used at the property level to describe the property's data type. In the form settings you pass the input type, which uses just the key 'type'. To pupulate it after the form was created use:I have tried overwriting these detials into the constructor of the model but this doesnt seem to work. static::$_properties = static::country_dropdown_list();
$form->field('country_id')->set_option($ops)
'gender' => array( 'data_type' => 'varchar', 'label' => 'Gender', 'form' => array('data_type' => 'select', 'options' => array('m' => 'Male', 'f' => 'Female')), 'validation' => array('required'), ),
'gender' => array( 'data_type' => 'varchar', 'label' => 'Gender', 'form' => array('type' => 'select', 'options' => array('m' => 'Male', 'f' => 'Female')), 'validation' => array('required'), ),
'country_id' => array( 'data_type' => 'char', 'label' => 'Country ID', 'validation' => array('required'), 'form' => array( 'type' => 'select', 'options' => array(), 'class' => 'xlarge', ), ),And populating the country dropdown:
public static function _init() { $countries = \Model_Country::find('all'); foreach ($countries as $c) { $data[$c->id] = $c->country_name; } static::$_properties['country_id']['form']['options'] = $data; }As always, thanks for sharing your code and thoughts.
Statically define the properties (like above), and add methods to the model to load data into the properties for the select options (which are only needed in crud operations on the model, there is no need to load them in all other uses of the model).
method_exists($this->model, 'set_options') and $this-model->set_options();which populates the select options at runtime. The Crud controller then creates a fieldset from the model, my controller may add custom fields to that, and passes the fieldset to a standard view which generates the form.
It looks like you're new here. If you want to get involved, click one of these buttons!