Conditional display of form fields in PloneFormGen

728 Views Asked by At

Plone 4.3.3

PloneFormGen 1.7.16

I would like to display or hide a set of fields based on the user's selection from a Selection field. The Fieldset Begin and Fieldset End elements do not answer this, so I am left with an Overrides Enabling Expression for each field. What is the TALES incantation for testing the current value of a form field? Will PloneFormGen dynamically test so that the fields are shown/hidden upon the user changing the selection?

Thank you,

Chuck

1

There are 1 best solutions below

0
jumois On

This can be done with JavaScript: 1 and 2.

First create JavaScript file to the forms folder:

  1. Browse to the forms folder through ZMI, or append "/manage_main" to the forms folder URL.
  2. Select a File as a new content type to add
  3. Define ID and set the content type as "text/plain"
  4. Edit the file

Sample js to hide field based on the selection of an another field (modified from 2): (Note: requires selection-list field)

<script type="text/javascript">
    $(document).ready(function() {
       if($("#selection-fieldname").val() != "I like this") {
           $('#fieldname-to-hide').parent().hide();
       }
       $("#selection-fieldname").change(function() {
           if ($(this).val() == "I like this") {
               $('#fieldname-to-hide').parent().slideDown();
           } else {
               $('#fieldname-to-hide').parent().slideUp();
           }
    });
});
</script>

Second inject the js to the form's header:

  1. Go to the edit of the forms folder.
  2. In Overrides tab, type 'here/your-script-ID' to the Header Injection field.

Third (optional) make the dynamically hidden field required only when it is visible.

  1. Go the field edit and clear the Required check box if checked.
  2. In Overrides tab, add a custom field validator:

    python: test(request.form['selection-fieldname'] != 'I like this' or value, False, 'Required when I like this is selected.')