Form Multi - Values blank?

edited March 2014 in Modules
Any reason this shouldn't work within a simple module?

function form_fields($values = array(), $related_fields = array()) {
$yearsValue = explode(",",$values['years']);
$fields = parent::form_fields($values, $related_fields);
$years = array(2008 => 2008, 2009 => 2009, 2010 => 2010, 2011 => 2011, 2012 => 2012, 2013 => 2013);
$fields['years'] = array('type' => 'multi', 'options' => $years, 'value' => $yearsValue);
return $fields;
}
function on_before_clean($values = array()){
if($values['years']) {
$values['years'] = implode(",",$values['years']);
}
return $values;
}

It's saving in the database as expected, however the value will just not work. Pretty confused!

Comments

  • edited 2:08PM
    Setting the value in the form_fields method will only work when first creating a record. Editing the record will pull the information from your module's edit_method which by default is the find_all_array method on your model. You can try either overwriting that method in your model, or setting the "edit_method" module parameter to a different method on your model.

    Additionally, you could try setting the "years" field as "serialized" on your module's model which will automatically JSON encode and decode:
    .... public $serialized_fields = array('years');

    Also, on line one in the method, you should first check for the value of $values['years'] since it won't exist when creating a new record.
Sign In or Register to comment.