Preprocessing form data before saving to the database

149 views
Skip to first unread message

Ryan Matthews

unread,
Jan 27, 2016, 7:04:26 PM1/27/16
to Joomla! General Development
Cross post from StackExchange... 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.

PGS

unread,
Jan 28, 2016, 3:48:15 AM1/28/16
to Joomla! General Development
Checkboxes do not get posted when they are unchecked.
They get posted only when they are checked.

Ryan Matthews

unread,
Jan 28, 2016, 6:14:44 AM1/28/16
to Joomla! General Development

Understood- the values I'm sending over are indeed checked. The above was just a sample.


On Thu, Jan 28, 2016, 3:48 AM PGS <panagiot...@gmail.com> wrote:
Checkboxes do not get posted when they are unchecked.
They get posted only when they are checked.

--
You received this message because you are subscribed to a topic in the Google Groups "Joomla! General Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/joomla-dev-general/EhH0qoRk1uw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.

seraphim

unread,
Jan 28, 2016, 8:45:05 AM1/28/16
to joomla-de...@googlegroups.com
Maybe you don.t get it because its not in the jform array.

use: name=jform[dancer_id][]

Op 28-01-16 om 01:04 schreef Ryan Matthews:
Cross post from StackExchange... 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):
&lt
;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.
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Ryan Matthews

unread,
Jan 31, 2016, 7:39:27 PM1/31/16
to Joomla! General Development, sera...@glockenbach.net
That solved it!! I've updated my fieldname.php so it uses the format you've suggested:

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

And I can confirm that my public function save($data) { ... } is getting the correct data as I'm seeing the following if I do a print_r($data):

Array ( [entry_id] => 11635 [studio_id] => 126 [location_id] => 30 [perf_title] => Amazing Twinkle [choreographer] => Bob [dancer_id] => Array ( [0] => 2261 [1] => 2270 ) [avg_age] => 11 [entrydiv_id] => 1 [perflvl_id] => 10 [dancecat_id] => 19 [agediv_id] => 24 [dvd_auth] => 1 [breaktime] => 0 [notes] => [catid] => 0 [dateadded] => 2015-12-19 [datemodified] => 2016-01-31 [published] => 1 [mss] => 0 [params] => Array ( [show_category] => ) [asset_id] => [rules] => Array ( [core.delete] => Array ( [6] => 1 ) [core.edit] => Array ( [6] => 1 [4] => 1 ) ) [tags] => )

Then run my code:

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

 
}


It provides the correct end result:

Array ( [entry_id] => 11635 [studio_id] => 126 [location_id] => 30 [perf_title] => Amazing Mayzie [choreographer] => Erin Koehler [dancer_id] => 2261,2270 [avg_age] => 11 [entrydiv_id] => 1 [perflvl_id] => 10 [dancecat_id] => 19 [agediv_id] => 24 [dvd_auth] => 1 [breaktime] => 0 [notes] => [catid] => 0 [dateadded] => 2015-12-19 [datemodified] => 2016-01-31 [published] => 1 [mss] => 0 [params] => Array ( [show_category] => ) [asset_id] => [rules] => Array ( [core.delete] => Array ( [6] => 1 ) [core.edit] => Array ( [6] => 1 [4] => 1 ) ) [tags] => )

And saves to the database :)

My function ends off with the following, btw:

return parent::save($data);

THANK YOU SO MUCH!

Ryan

On Thursday, 28 January 2016 08:45:05 UTC-5, seraphim wrote:
Maybe you don.t get it because its not in the jform array.

use: name=jform[dancer_id][]

Op 28-01-16 om 01:04 schreef Ryan Matthews:
Cross post from StackExchange... 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):
&lt
;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.
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.

seraphim

unread,
Feb 1, 2016, 10:34:33 AM2/1/16
to joomla-de...@googlegroups.com
You're welcome :-)

Op 01-02-16 om 01:39 schreef Ryan Matthews:
-- You received this message because you are subscribed to the Google Groups "Joomla! General Development" group. To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com. To post to this group, send email to joomla-de...@googlegroups.com. Visit this group at https://groups.google.com/group/joomla-dev-general. For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages