2
votes

I have created a component. I try to store the data from a xml form into the database. This works for all text fields but the field "checkboxes" makes problems.

Here is the xml code:

<field name="color" type="checkboxes" label="COM_COLOR" multiple="true">
     <option value="1">red</option>
     <option value="2">blue</option>
     <option value="3">green</option>
     <option value="4">yellow</option>             
</field>

code in the edit.php

<?php echo $this->form->getInput('color'); ?>

It is exactly the same like the Joomla Documentation: http://docs.joomla.org/Checkboxes_form_field_type

But if I click to save the checkbox values was not stored in database. Can anyone help? thanks

Joomla version: 3.2.1

EDIT: NOW IT WORKS! Write the following code into the php file where the JTable was extend. (your_component/tables/your_file.php)

public function store($updateNulls = true)
{
    JArrayHelper::toString($this->color);
    $this->color= implode(',', $this->color); 
    return parent::store($updateNulls);
} 
2
Can you clarify if you are doing this inside or outside of a fieldset and if the field or fieldset name matches a field name in the database? - Elin

2 Answers

0
votes

In the documentation that you provided states clearly:

Note: [...] It will create checkboxes for you, and submit their values in form of an array, but it will not store them in the database.

To retrieve the values you have to do as it says in the documentation that you provided, fetch the entire array of the checkboxes values and process them separately. Otherwise you can create individual checkboxes that will work out of the box more info here.

0
votes

In Joomla > 2.5 you can store it as a comma separated string. You have to override your default save function adding a line for creating comma separated string.

For example:

function save(){    

  $data = JRequest::getVar( 'jform', null, 'post', 'array' );
  $checkbox_options = $data['checkbox_field_name'];

  $data['checkbox_field_name']=implode(",", $checkbox_options);

  JRequest::setVar('jform', $data );

  return parent::save();
}

Hope this help to save someones' time. :) Happy coding..!!!