How do you set default values for Template Field (FuelCMS 1.0)
I'm using the Template field and everything for the 'add' portion is working great, multiple items are being inserted into the right tables, all data is saved correctly...the relevant code below.
$fields['date_ranges'] = array(
'display_label' => TRUE,
'type' => 'template',
'label' => 'Date Ranges',
'non_sortable' => TRUE,
'fields' => array(
'Dates' => array('class' => 'no_editor datepick' ),
),
'add_extra' => TRUE,
'repeatable' => TRUE
);
My question is about when I am editing. How do I get the multiple instances of the template to display and work like it does during 'add'. I don't want to put them in 'fields' because then clicking 'add another' will duplicate all the fields and they won't be stored in the same array when submitted.
Any help would be appreciated.
Comments
For most form fields I would set the 'value' with a string or array. But I don't see how to create and populate the template field values when editing a record.
public $serialized_fields = array('dates');
Another option is to either overwrite the find_one_array method on your model (inherited from the MY_Model class), or create a new method on your model to return the data in a specific format and specify the "edit_method" for the module in your MY_fuel_modules.php config file like so:
$config['modules']['news'] = array( 'edit_method' => 'my_find_one_array', .... );
Does that make sense?
Maybe I am not using the Template type or add_extra/repeatable options correctly or have different expectations of how it should work.
But if I am able to create a form field that I can duplicate and add content to within the CMS, then capture this dynamically added form field on submit. I assumed there was an easy way on the editing of that form, to show the fields in the same way as when they were created.
I will go back and look at how I am handling the user being able to enter an unlimited number of date ranges and possibly re-work how it is done.
Thanks for your time.
function my_find_one_array($where, order = NULL){ $data = parent_find_one_array($where, $order); $CI =& get_instance(); $CI->load->model('my_other_model'); $related_dates = $CI->my_other_model->find_all_array(array('foreign_id' => current($where)); $data['dates'] = $related_dates; return $data; }
That Template field is looking for an array of associative arrays for it's value.
For completeness sake if anyone else is looking to do multiple instances of dates (or anything really) the model code is below (pseudo code really, cleaned out unnecessary stuff for the example):
At the top of the model:
public $serialized_fields = array('date_ranges');
In the table for this model, I have a 'text' field called date_ranges that is used to store the data in a serialized array. I am not using the built in date field because I wanted to use the jquery datePick plugin that allows ranges within a single text box.
function form_fields($values = array()) { $fields = parent::form_fields(); $fields['date_ranges'] = array( 'display_label' => TRUE, 'type' => 'template', 'label' => 'Date Ranges', 'non_sortable' => TRUE, 'fields' => array( 'Dates' => array('class' => 'no_editor datepick'), ), 'repeatable' => TRUE ); $this->form_builder->set_fields($fields); return $fields; }
I need the dates in a separate table for searching purposes, so before the record is updated:
function on_before_clean($values) { //remove existing dates from dates_table $this->db->query('delete from dates_table where foreign_id = '.$values['id']); foreach($values['date_ranges'] as $date_range){ $dates = explode(" - ", $date_range['Dates']); $this->db->query('insert into dates_table (foreign_id, date_start, date_end) values ('.$values['id'].',"'.$dates[0].'","'.$dates[1].'")' ); } return $values; }