I have a SQLFORM.factory form that has multiple fields, one hidden by default in the view (via jquery) and shown when another field, a dropdown select, changes to some value. In the code below, field_A is shown if field_B 'Choice 2' is selected.
The problem is that fields (eg field_A) which are shown only after the dropdown is
changed are not being validated properly. Here, field_B and field_C are validated just fine (they are never hidden), yet field_A is not validated in the code below - it could be anything and the form will still submit.
form = SQLFORM.factory(
Field( 'field_A', requires=IS_IN_SET(['Choice 1', 'Choice 2'], multiple=False, zero='Please choose') ),
Field( 'field_B', requires=IS_IN_SET(['Choice 1', 'Choice 2'], multiple=False, zero='Please choose') ),
Field( 'field_C', requires=IS_IN_SET(['Choice 1', 'Choice 2'], multiple=False, zero='Please choose') )
)
def cvalidator(form):
if form.vars.field_B == 'B Choice 2':
if not form.vars.field_A: form.vars.field_A.errors = 'Fill it in'
if form.accepts(request.vars, session, onvalidation=cvalidator): pass
<script>
jQuery(document).ready(function() {
jQuery('#no_table_field_A__row').hide();
jQuery('#no_table_field_B').change(function(){
if( jQuery('#no_table_field_B option:selected').text()=='Choice 2' ){
jQuery('#no_table_field_A__row').show();
}else{
jQuery('#no_table_field_A__row').hide();};
});
});
</script>