7

I tried using the showon form field attribute in a front-end form with no working results.

Not even the example given here is working: https://docs.joomla.org/Form_field#Showon

Is there anything missing, do I have to include a certain script manually when I wanna use it in the front-end?

I'm using joomla 3.4.1, front end is a T3 template, in case that matters.

Any ideas or solutions ?

EDIT: The form

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset name="main">  
        <field name="foo" type="list"  >
            <option value="1">JYES</option>
            <option value="0">JNO</option>
        </field>

        <field name="bar" type="text" showon="foo:1" />

        <field name="foo2" type="text"  />

        <field name="bar2" type="text" showon="foo2:1" />
    </fieldset>  
</form>

The template

<?php
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');

?>

<form action="...>"
      method="post" 
    onsubmit="return false;" 
    name="adminForm" id="adminForm" class="form-validate">
    <div class="form-horizontal adminform">
        <fieldset class="">
            <legend><?php echo 'test'; ?></legend>
                <div class="span6">
                    <?php foreach ($this->form->getFieldset() as $field): ?>
                        <div class="control-group">
                            <div class="control-label"><?php echo $field->label; ?></div>
                            <div class="controls"><?php echo $field->input; ?></div>
                        </div>
                    <?php endforeach; ?>
                </div>
        </fieldset>
    </div>
    <?php echo JHtml::_('form.token'); ?>
</form>

EDIT 2:Found a solution in joomla core files

if the template code above is updated from:

<div class="span6">
    <?php foreach ($this->form->getFieldset() as $field): ?>
        <div class="control-group">
            <div class="control-label"><?php echo $field->label; ?></div>
            <div class="controls"><?php echo $field->input; ?></div>
        </div>
    <?php endforeach; ?>
</div>

To:

<div class="span6">
    <?php foreach ($this->form->getFieldset() 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; ?>>
            <div class="control-label"><?php echo $field->label; ?></div>
            <div class="controls"><?php echo $field->input; ?></div>
        </div>
    <?php endforeach; ?>
</div>

it works

SO am I supposed to manually add this part all the time?

Docu doesn't state anything about this, I need to load the cms.js file manually and build the control-grouptag myself? Seems strange

2
  • Can you please show the code you're using?
    – Lodder
    May 11, 2015 at 0:11
  • I added the form and the tmpl code
    – Andreas
    May 11, 2015 at 1:13

2 Answers 2

2

I guess it's a problem related to system core, Your template/Joomla don't load JS for that functionality, just add JS /media/jui/js/cms.js in your controller where you load form.

regards

1
  • Adding just the script does not work, I also have to actually add the code to create the correct parent control-group div as my EDIT 2 shows. Neither action is described in the docu.
    – Andreas
    May 11, 2015 at 15:20
2

It should work as long as your form is generated using JForm. If you build your HTML for the form yourself, then you may have to manually add the needed code and markup. Try rendering the field using <?php echo $this->form->renderField('title'); ?> or in case of the foreach you could use <?php echo $this->form->renderField($field->fieldname); ?> This seems to work in my case.

2
  • I'll give it a try as son as I get some time. I do generate the form the 'standard' way as shown in most tutorials. And I'm sure I used it once before, to try it out and had no need for those hoops. I suspect (really only guessing at this point) it has to do with the updates. This Joomla was a 3.0 once and has been updated over time to 3.4
    – Andreas
    Jun 7, 2015 at 5:59
  • Updates shouldn't make a difference. However using JForm to generate the form does make a difference.
    – Bakual
    Jun 7, 2015 at 19:06

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.