1

Making form with deform and whould like to change the pageShema class depending on the choices made by the user. Ex. if he selects option 1 from selectwidget, show him one set of fields, if case of other choice - another. How to do this?

1
  • Please add some code to the question show we get a hang what's going on Commented Dec 12, 2015 at 3:53

1 Answer 1

5

You can use jquery and select with the "name" attribute. Also, you can use the jquery "parent()" function to get the container of the input you are interested in showing/hiding.

For instance, in your schema do something like:

# This is the "<select>"
choices = (('yes','Yes'),
           ('no', 'No'))
bar = colander.SchemaNode(colander.String(),
                          widget=deform.widget.SelectWidget(values=choices))

# This is the input that should appear only when the "yes" value is selected
foo = colander.SchemaNode(colander.String())

Then, in your template, add somethig like:

<script>
$( document ).ready(function() {

// ensure that the "foo" input starts hidden
var target = $("input[name=foo]").parent().parent();
target.hide();

$("select[name=bar]").on('change', function(){
    var valueSelected = this.value;
    if (valueSelected == "yes") {
      target.show();
    } else {
      target.hide();
    }
});



});
</script>
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.