How to implement dynamic fields on a Back End Joomla! component ?

990 views
Skip to first unread message

Stephane B

unread,
Jun 23, 2015, 5:15:44 PM6/23/15
to joomla-de...@googlegroups.com
Hi there,

I start Joomla! (3.x) component development recently and I'm facing an issue that online tutorials can't solve for me.

So far, I did very simple Back End forms following the examples shown in this tutorial : https://docs.joomla.org/J3.x:Developing_a_MVC_Component.

But now, I need to have a form where there is general and static information, plus an unknown amount of dynamic fields.

Concretely, I have an "Article" with :

- Id,
- Label,
- Description.

Here are the static fields.

And each "Article" is linked to one or many "Price". A "Price" is defined as below :

- Id,
- Label,
- Amount,
- User_Group.


Is there a way to deal with my problem ? Cause I've no idea of how to do this with the classic XML static Form definition.

Your help would be very very helpful and I thank you in advance !!

Regards,
Stéphane

Hùng Trần

unread,
Jun 23, 2015, 9:42:58 PM6/23/15
to joomla-de...@googlegroups.com
Hi Stephane,

You can modify your form in getForm function to add new fields. I often do that like this.

class MyComponentModelMyItem extends JModelAdmin
{
    public function getForm($data = array(), $loadData = true)
    {
        // Get the form.
        $form = $this->loadForm('com_mycomponent.myitem', 'myitem', array('control' => 'jform', 'load_data' => $loadData));

        if (empty($form))
        {
            return false;
        }

        // Modify the $form object here to change field attributes or add/remove fields.

        $myFields = array(
            // Array of dynamic fields.
        );

        // Add new fields to a new fieldset.
        $xml = '<fieldset name="custom_fields">';

        foreach ($myFields as $field)
        {
            $xml .= '<field name="' . $field->name . '" type="' . $field->type . '" label="' . $field->label . '" description="' . $field->desc . '" />';
        }

        $xml .= '</fieldset>';

        // Add new fieldset to form.
        $element = new SimpleXMLElement($xml);
        $form->setField($element);

        // We can also change field attribute here.
        $form->setFieldAttribute('somefield', 'required', 'false');

        // Or remove a field.
        $form->removeField('removeme', 'somefieldset');

        $data = $this->loadFormData();

        $form->bind($data);

        return $form;
    }
}

To validate your dynamic fields you also need to do the same to validate() function, otherwise the new fields are not there to validate.

    public function validate($form, $data, $group = null)
    {
       // Add dynamic fields or modify existing fields here before validating the inputs.

        return parent::validate($form, $data, $group);
    }

I know it is really good, if anybody has a better solution please let us know.

Regards,
Hung



--
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 an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.

Hannes Papenberg

unread,
Jun 24, 2015, 2:53:44 AM6/24/15
to joomla-de...@googlegroups.com
If you are talking about a component that you didn't write yourself, you
might be able to modify the XML via a plugin, like the user profile
plugin does.

Hannes

Stephane B

unread,
Jun 24, 2015, 11:39:19 AM6/24/15
to joomla-de...@googlegroups.com
Hi,

I'm talking about a component that I write myself :) 

Stephane B

unread,
Jun 24, 2015, 11:41:58 AM6/24/15
to joomla-de...@googlegroups.com
Hi Hung,

Thanks for your answer. But this solution is still quite a bit "static" as fields are not add with JavaScript functions. It surely works, but I was wondering if there was a solution where we can add dynamic fields with javascript, when users click on a (+) button, for example.

Best regards !
Stéphane
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.

Walt Sorensen

unread,
Jun 24, 2015, 1:54:06 PM6/24/15
to joomla-de...@googlegroups.com
There are two existing things that might help with what your working on.
There is the showon attribute that will help you hid/show fields based on user selection (since 3.2.4)
There is also the "repeatable" field type that will dynamically add more field when a +/- button is selected. (since 3.2)
(one caveat on repeatable fields, it's currently broken for some field types like User, Calendar, etc. Basically any field type that includes JS logic like a modal box. Currently works great for things like text boxes)


Stephane B

unread,
Jun 25, 2015, 9:16:36 AM6/25/15
to joomla-de...@googlegroups.com
Great. I should be able to create my component with all these answers. Thanks a lot !! :)
Reply all
Reply to author
Forward
0 new messages