2

I have a series of checkboxes on my form that together form an array when POST'ed. I want to save this data as a string instead (e.g., "##,##,##") before committing to the database.

Here's what I have in the \models\componentname.php:

public function save($data)
{
    $input = JFactory::getApplication()->input;
    $filter  = JFilterInput::getInstance();

    if(isset($data['dancer_id'])) {     
        $data['dancer_id'] = implode(",", $data['dancer_id']);
    }

    if (parent::save($data))
    {
        return true;
    }

    return false;
}

And here is the custom field type I've created (\models\fields\fieldname.php):

<div class="dancer"><input id="dancer_id" type="checkbox" name="dancer_id[]" value="XXX" />XXX</div> 

All other fields in the form save except for this one. Any thoughts? Also I'm not sure how the $data is being passed to the save() function ... maybe I've missed a call somewhere? To get this far, I followed the HelloWorld MVC component tutorial on the Joomla site and the response in this related thread.

2 Answers 2

1

Have you tried retrieving the form input directly without using JFilterInput?

Try this:

replace:

$filter  = JFilterInput::getInstance();

with:

$dancers = $input->get('dancer','','array');

you can then do

$data['dancer_id'] = implode(",", $dancers);

From there you can finish up with your save methods

2
  • Turns out I didn't have my custom field added to the jform array ... I've edited my original question with my solution. Thanks for your help!
    – Mhaddy
    Commented Feb 6, 2016 at 19:08
  • No worries, from the field name I was unaware you were using jform or I'd have pointed that out lol.
    – Terry Carter
    Commented Feb 7, 2016 at 1:01
1

I've figured it out with the help of someone on the Joomla DEV Google Groups (details): I did not have my custom field dancer_id added to the jform array like so:

<input id="dancer_id" type="checkbox" name="jform[dancer_id][]" value="2261" checked>

Once I did so, it all worked. Details linked above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.