3

I'm developing a new Joomla component where I have a JAdminForm called course.xml. This form has several text fields / text areas that will contain meta titles / tags for an item.

However, when submitted and saved to the database, I want to JSON encode these metadata fields for storage in a single database column, metadata. Similarly, when the form loads I'd like to JSON decode these out into their respective form fields for (re)editing.

There seems to be a few different approaches to solve this:

  • Override JControllerForm save() / bind()
  • Override JModelAdmin save()
  • Use plugin events like onContentPrepareForm

My gut feeling is that I'd rather override than use plugins (seems like a sledgehammer cracking a nut), but looking to see what is 'standard'. This also seems like it'd be a fairly common use case (where the XML form used by JForm doesn't exactly match the database), but I can't find a definitive resource on how to approach / implement.

2 Answers 2

3

I would not use the controller - as controllers should be left to simply creating the model, view, linking them together and dealing with any redirect.

As you say, the plugin way is going to produce additional overhead on all form's which doesn't make sense to me.

So, my preference is the model, in its save() function. If you look at administrator/components/com_content/models/article.php this is where the data is manipulated before being bound & saved to the table.

On edit, I would use the model's getItem() method to re-convert the data back into the values required by JForm.

hth Rob

0
2

Been doing some further research on this...

  • JTable bind() seems to be the preferred method based on the Joomla Component Creator to manipulate data before saving
  • JModelAdmin getItem() seems to make sense for manipulating data before display in the admin form

Your Answer

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

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