Showon Option in Form Field

642 views
Skip to first unread message

Glenn Arkell

unread,
Mar 25, 2016, 3:35:21 AM3/25/16
to Joomla! General Development
I'm guessing I am missing some small part of the process here. . .  I'm trying to use the showon option in a field.  I have set the following XML in my form;

        <field name="p_same" type="radio" class="btn-group btn-group-yesno"
            label="COM_MYCOMP_FORM_LBL_HOSTDETAIL_P_SAME"
            description="COM_MYCOMP_FORM_DESC_HOSTDETAIL_P_SAME" >
            <option value="1">JYES</option>
            <option value="0">JNO</option>
        </field>
   
        <field name="h_post_ad1" type="text" showon="p_same:0"
            label="COM_MYCOMP_FORM_LBL_HOSTDETAIL_H_ADDRESS1"
            description="COM_MYCOMP_FORM_DESC_HOSTDETAIL_H_ADDRESS1" />
   
        <field name="h_post_ad2" type="text" showon="p_same:0"
            label="COM_MYCOMP_FORM_LBL_HOSTDETAIL_H_ADDRESS2"
            description="COM_MYCOMP_FORM_DESC_HOSTDETAIL_H_ADDRESS2" />
   
        <field name="h_post_city" type="text" showon="p_same:0"
            label="COM_MYCOMP_FORM_LBL_HOSTDETAIL_H_CITY"
            description="COM_MYCOMP_FORM_DESC_HOSTDETAIL_H_CITY" />
   
Do I need to do anything in the view to make the postal address details hide when "p_same" = 1?  I haven't found any doco on this so far so if there is any, please shove me in the right direction.  Cheers.

Troy Hall

unread,
Mar 25, 2016, 10:47:59 AM3/25/16
to joomla-de...@googlegroups.com
There is one but its well hidden.... nope, you've done all you need to do
Bear
--
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.

Glenn Arkell

unread,
Mar 25, 2016, 4:01:21 PM3/25/16
to Joomla! General Development
Thanks Bear, any chance of a link to this well hidden doco?  Cheers.

Glenn

Troy Hall

unread,
Mar 25, 2016, 5:20:38 PM3/25/16
to joomla-de...@googlegroups.com
https://docs.joomla.org/Form_field at the extreme bottom of the page
Bear

Glenn Arkell

unread,
Mar 25, 2016, 5:32:50 PM3/25/16
to Joomla! General Development
See it just proves that I don't read the entire page as I've used this page often.  Thanks again.  As it doesn't work for me I'm guessing something else is wrong.  More investigation needed.  Cheers.

Glenn

brian teeman

unread,
Mar 25, 2016, 6:12:07 PM3/25/16
to Joomla! General Development
I just pasted that code into one of my forms and on clicking Yes the fields disappear - is that not what you want?

Glenn Arkell

unread,
Mar 25, 2016, 6:14:49 PM3/25/16
to Joomla! General Development
Hi Brian, Yes that's what I want but I'm not getting it.

brian teeman

unread,
Mar 25, 2016, 6:20:44 PM3/25/16
to Joomla! General Development
Worked for me - just copy pasting your snippet into one of my forms


showon.gif

brian teeman

unread,
Mar 25, 2016, 6:23:47 PM3/25/16
to Joomla! General Development

Glenn Arkell

unread,
Mar 26, 2016, 1:08:54 AM3/26/16
to Joomla! General Development
Now you're just showing off. . . so what am I missing?  Currently using 3.5.0, component originally created with Component Creator, view is using bootstrap tabs, but both trigger field and display fields within the same tab, all fields in the same fieldset????

Got me beat.

brian teeman

unread,
Mar 26, 2016, 6:09:42 AM3/26/16
to Joomla! General Development
Almost the exact same scenario as mine

If you email me the component I will take a look (brian at teeman dot net)

Troy Hall

unread,
Mar 26, 2016, 9:39:50 AM3/26/16
to joomla-de...@googlegroups.com
if the fieldset your testing against is not from the same fieldset as the showon usage then it will fail.  if you send me your xml I'll take a look
Bear



On 3/25/2016 17:23, 'brian teeman' via Joomla! General Development wrote:

Glenn Arkell

unread,
Mar 26, 2016, 5:13:14 PM3/26/16
to Joomla! General Development
Hi Bear,

Thanks, no it's all in the one fieldset.  I had been reading past posts around having the showon in different fieldsets so I was conscious of making sure they were in the one area.  I think it is definitely something in my view code that is screwing it up and I need to review this a little closer.  Cheers.

Glenn

Troy Hall

unread,
Mar 26, 2016, 7:54:42 PM3/26/16
to joomla-de...@googlegroups.com
np, let me know if you need my eyes.
Bear

Glenn Arkell

unread,
Mar 27, 2016, 2:28:31 AM3/27/16
to Joomla! General Development
well a quick review and discussion around the view I was using, and I was explicitly itemising fields whereas using the XML for showing these fields is a better option (thanks to guidance from Brian Teeman).  So by leaving the XML code as above and instead of having my view (default.php) coded like:

    <div class="control-group">
        <div class="control-label"><?php echo $this->form->getLabel('p_same'); ?></div>
        <div class="controls"><?php echo $this->form->getInput('p_same'); ?></div>
    </div>
    <div class="control-group">
        <div class="control-label"><?php echo $this->form->getLabel('h_post_ad1'); ?></div>
        <div class="controls"><?php echo $this->form->getInput('h_post_ad1'); ?></div>
    </div>
    <div class="control-group">
        <div class="control-label"><?php echo $this->form->getLabel('h_post_ad2'); ?></div>
        <div class="controls"><?php echo $this->form->getInput('h_post_ad2'); ?></div>
    </div>

Use the JLayoutHelper to render the fields of the specific fieldset - where the fieldset name is defined as "basic", like this.

    <fieldset class="adminform">
        <?php
            $this->fieldset = 'basic';
            $html = JLayoutHelper::render('joomla.edit.fieldset', $this);
            echo $html;
        ?>
    </fieldset>

I hope this helps others and thanks to Brian and Bear for being ready to assist others so generously.  Cheers.

Glenn

ALFsoft

unread,
Mar 28, 2016, 7:39:39 AM3/28/16
to joomla-de...@googlegroups.com
Totally missed/overlooked this option…
But makes me want more ;-)

Is there a way to use something similar to show/hide a whole fieldset based on the value of a field from another fieldset ?

Alfred

Op 27 maart 2016 bij 08:29:27, Glenn Arkell (glenn...@gmail.com) schreef:

Troy Hall

unread,
Mar 29, 2016, 8:21:14 AM3/29/16
to joomla-de...@googlegroups.com
nope.. not in core.
Bear

Bakual

unread,
Mar 29, 2016, 3:10:37 PM3/29/16
to Joomla! General Development
I tried it for the component options view. It just needs a bit of code in the layout: https://github.com/joomla/joomla-cms/compare/staging...Bakual:FieldsetShowon
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.

Alfred Vink

unread,
Mar 29, 2016, 3:26:46 PM3/29/16
to joomla-de...@googlegroups.com
Looks promising, hope it makes it to an upcoming version soon.

Met vriendelijke groeten,
Alfred Vink

Op 29 maart 2016 bij 21:11:34, Bakual (bakual...@gmail.com) schreef:

To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Troy Hall

unread,
Mar 29, 2016, 10:16:29 PM3/29/16
to joomla-de...@googlegroups.com
would be a awesome step forward
Bear
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Bakual

unread,
Mar 30, 2016, 6:31:44 AM3/30/16
to Joomla! General Development
I can do a PR from it. But it would only be for component options. Fieldsets rendered by extensions would need their own code, because fieldsets aren't rendered by JForm, they're rendered by the extension layout.

Bakual

unread,
Mar 30, 2016, 8:45:16 AM3/30/16
to Joomla! General Development

ALFsoft

unread,
Mar 30, 2016, 10:48:19 AM3/30/16
to joomla-de...@googlegroups.com
Works like a charm, thanks !

Alfred

Op 30 maart 2016 bij 14:46:12, Bakual (bakual...@gmail.com) schreef:

To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Glenn Arkell

unread,
Mar 31, 2016, 1:35:36 AM3/31/16
to Joomla! General Development
I've had the backend working no problems, but I'm still struggling with the frontend.  Tried the same code as used in the backend with no success.  Tried the following:

My XML definitions
    <fieldset name="padinfo">

        <field name="p_same" type="radio" class="btn-group btn-group-yesno" default="0"
            label="COM_MYCOMPONENT_FORM_LBL_HOSTDETAIL_P_SAME"
            description="COM_MYCOMPONENT_FORM_DESC_HOSTDETAIL_P_SAME" >

            <option value="1">JYES</option>
            <option value="0">JNO</option>
        </field>
   
        <field name="h_post_ad1" type="text" showon="p_same:0"
            label="COM_MYCOMPONENT_FORM_LBL_HOSTDETAIL_H_ADDRESS1"
            description="COM_MYCOMPONENT_FORM_DESC_HOSTDETAIL_H_ADDRESS1" />

   
        <field name="h_post_city" type="text" showon="p_same:0"
            label="COM_MYCOMPONENT_FORM_LBL_HOSTDETAIL_H_CITY"
            description="COM_MYCOMPONENT_FORM_DESC_HOSTDETAIL_H_CITY" />
   
        <field name="h_post_pcode" type="text" showon="p_same:0"
            label="COM_MYCOMPONENT_FORM_LBL_HOSTDETAIL_H_POSTCODE"
            description="COM_MYCOMPONENT_FORM_DESC_HOSTDETAIL_H_POSTCODE" />
   
    </fieldset>

In my view I have the following:

            <?php echo JHtml::_('bootstrap.addTab', 'myTab', 'padinfo', JText::_('COM_MYCOMPONENT_TITLE_PADDRESS', true)); ?>
                <div class="row-fluid">
                    <div class="span10 form-horizontal">

                        <?php foreach ($this->form->getFieldset('padinfo') as $field): ?>
                            <?php
                            $class = '';
                            $rel = '';
                            if ($showon = $field->getAttribute('showon')) {
                                JHtml::_('jquery.framework');
                                JHtml::_('script', 'jui/cms.js', false, true);
                                $id = $this->form->getFormControl();
                                $showon = explode(':', $showon, 2);
                                $class = ' showon_' . implode(' showon_', explode(',', $showon[1]));
                                $rel = ' rel="showon_' . $id . '[' . $showon[0] . ']"';
                            }
                            ?>

                            <div class="control-group<?php echo $class; ?>"<?php echo $rel; ?>>
                                <?php echo $this->form->renderField($field->fieldname); ?>
                            </div>
                        <?php endforeach; ?>

                    </div>
                </div>
            <?php echo JHtml::_('bootstrap.endTab'); ?>


Any advice appreciated.  Cheers.

Glenn

Bakual

unread,
Mar 31, 2016, 4:03:46 AM3/31/16
to Joomla! General Development
If you tested it, please report your results using issues.joomla.org. Otherwise it never gets life :)
Reply all
Reply to author
Forward
0 new messages