Friday, June 1, 2012

ploneformgen show/hide fields based on selection field

We wanted to show/hide some fields in our multistage form created in Plone via ploneformgen. It is a neat trick with jQuery and here is how we did it:

  • Open ZMI/yourSite
  • Navigate to the form folder
  • Add new Python script 
  • Click Add and edit
  • Paste the code

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

Now open the form->edit->overrides and in the Header Injections Field enter the id of the python script as:
here/scriptID

works like a charm :)

No comments:

Post a Comment