Guest
11 Sep 2013 11:54

I don't believe Cobalt can currently do this, but it would be nice to have conditional showing of fields. If someone click 'Yes' for a specific boolean field then an additional 2 fields appear for them to fill out. Or if someone selects 'Option 2' for this field than another field appears.

As I create more complex Types with Cobalt I believe this would be a handy feature for users entering content.

Last Modified: 09 Jun 2015


stblink VIP
Total posts: 501
11 Sep 2013 14:43

That idea already has several topics, old ones.

On C9 roadmap the conditional fields is not mentioned but it would be a great feature.


pepperstreet VIP
Total posts: 3,837
11 Sep 2013 17:40

FYI:

Topic was discussed in this C7 topic.

Related C8 topic by me with thoughts about possible client-side JavaScript solution.


Sergey
Total posts: 13,748
12 Sep 2013 02:00

This is very easy to do with custom template. You only add there JS script like that

(function($){

$('input[name^=jform\\[fields\\]\\[10\\]]').on('change', function(){

    if(this.value == -1) {

           $('#filed-23').show();

           $('#filed-25').hide();

    } else {

           $('#filed-23').hide();

           $('#filed-25').show();

    }

});

}(jQuery))

Something like this. Simple add on changed event to boolean field and show or hide some form elements.


stblink VIP
Total posts: 501
12 Sep 2013 08:26

And what happens if a conditional field that is hidden is set to required? Will it be processed as we press submit and error popsup or the 'required' will be ignored?


Guest
12 Sep 2013 12:51

I may try this javascript solution, thanks.


Sergey
Total posts: 13,748
15 Sep 2013 02:46

And what happens if a conditional field that is hidden is set to required? Will it be processed as we press submit and error popsup or the 'required' will be ignored?

Will be processed as usual.


klox7 VIP
Total posts: 914
29 Dec 2013 12:39

Did anyone try to do this for record (not edit form)?


klox7 VIP
Total posts: 914
10 Jan 2014 05:07
<?php if(isset($item->fields_by_id[ID_x]) && $item->fields_by_id[ID_x]->result == 'Yes'): ?>

<div id="<?php echo 'field-rslt-'.$item->fields_by_id[ID_y]->id; ?>" class="<?php echo $item->fields_by_id[ID_y]->params->get('core.field_class');?><?php echo ($item->fields_by_id[ID_y]->params->get('core.label_break') > 1 ? ' line-brk' : NULL) ?>">

                    <?php echo $item->fields_by_id[ID_y]->result; ?></a>

                </div>

            <?php endif; ?>

Sergey
Total posts: 13,748
13 Jan 2014 00:10

PHP will only load or hide based on saved value. I mean you will have to update page or submit form to trigger that.

This task have to be solved with javascript.

(function($){ $('select-element-id').bind('change keyup', function(){ if($(this).val() == 'Yes') { $('block-id1').hide(); $('block-id2').show(); } }); }(jQuery))

This is example block you have to add to formt template at very bottom.

This add listeners on change event on select field that should hide depends on its value.


nblgeoweb VIP
Total posts: 178
29 Apr 2014 18:32

I've been trying to get Sergey's earlier suggestion with code working on my form template. My knowledge of Javascript is pretty basic so I'm struggling a bit.

(function($){
$('input[name^=jform\\[fields\\]\\[92\\]]').on('change', function(){
if(this.value == "Training") {
$('#field-119').show();
} else {
$('#field-119').hide();
}
});
}(jQuery))

I'm using a select field for hiding/unhiding various fields in the form. The select list has an id of 92 and the field I'm hiding/unhiding has an id of 119. Does this code look ok and do I put it at the end of my form template or within tags.


Sergey
Total posts: 13,748
30 Apr 2014 03:24

Code looks ok. The only thing I would make one more function there to trigger it on edit article load.

(function($){
    var select = $('input[name^=jform\\[fields\\]\\[92\\]]');

    showHideFields(select);

    select.on('change', function(){
        showHideFields($(this));
    });

    function showHideFields(el){
        var tohide = $('#field-119');
        if(this.val() == "Training") {
            tohide.show();
        } else {
            tohide.hide();
        }
    }
}(jQuery))

And I would place it at the very bottom of your custom form template.


nblgeoweb VIP
Total posts: 178
30 Apr 2014 12:39

Thanks Sergey. That all makes sense. Thanks for the help. Eventually I'll learn enough javascript to be dangerous :)

I put the above code at the bottom of my form template below the three functions that are already there. If I display the page, all I see is the page heading and the Apply/Save/etc buttons. Any idea why?


Sergey
Total posts: 13,748
01 May 2014 10:36

If you give URL to that page I may have a look.


nblgeoweb VIP
Total posts: 178
01 May 2014 11:04

Unfortunately it's behind a firewall so you won't be able to access it. Is there any other information I can provide to help? I've been doing some playing around and it looks like it's only a problem when I use a self-executing function at the end of my template. Other functions seem to exist there fine.


Sergey
Total posts: 13,748
01 May 2014 16:09

Ue firebug to investigate. See if HTML is there and check if you have any JS error in console.


nblgeoweb VIP
Total posts: 178
02 May 2014 00:02

only HTML of what I see is there (form save/apply/etc buttons). When I use Firebug, I get the following error in the console:

ReferenceError: Joomla is not defined

Which seems to refer to Joomla.submitbutton in the beginning of a javascript (Joomla.submitbutton = function(task)). It appears to be a script defined in views/form/tmpl/default.php and views/record/tmpl/default_comment_form.php. I am using v8.531 by the way.


Sergey
Total posts: 13,748
02 May 2014 03:33

What is Joomla version?


nblgeoweb VIP
Total posts: 178
02 May 2014 07:17

Version 3.2.2


Sergey
Total posts: 13,748
02 May 2014 14:45

Hmmm. Then I do not know. See source if those fields on the page at all. First find out it is hidden or not there at all.


enocasino VIP
Total posts: 33
25 Sep 2014 00:12

This is what I put in my template

(function($){
    $('input[name^=jform\\[fields\\]\\[24\\]]').on('change', function(){
        if(this.value == 1) {
            $('#field-11').show();
        } else {
            $('#field-11').hide();
        }
    });
}(jQuery))

And this is the error I get which is talking about the very first line.

Parse error: syntax error, unexpected '$', expecting '&' or variable (T_VARIABLE) in /home/flyerfly/public_html/submissions/components/com_cobalt/views/form/tmpl/default_form_copyform.php on line 493

Powered by Cobalt