Grails Form Submission Fails with Dynamic Fields using JQuery -
i using jquery show/hide divs contain form fields. if value 1 drop down selected, div corresponding dependent form field shown, otherwise isn't. grails not allow form submitted unless dependent form field shown. experience similar? if so, work around did implement? thanks.
relevant code follows:
1. create.gsp <g:form url="[resource:dvdrequesterinstance, action:'save']" > <fieldset class="form"> <g:render template="form"/> </fieldset> <fieldset class="buttons"> <g:submitbutton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'create')}" /> </fieldset> </g:form> 2. _form.gsp <div class="fieldcontain ${haserrors(bean: dvdrequesterinstance, field: 'dvdrequesterposition', 'error')} required"> <label for="dvdrequesterposition" class="extended-label-width"> <g:message code="dvdrequester.dvdrequesterposition.label" /> </label> <g:select name="dvdrequesterposition" required="" id="position" from="${position.list()}" value="${fieldvalue(bean:dvdrequesterinstance, field:'dvdrequesterposition')}" optionkey="positionname" optionvalue="positionname" noselection="['':'select position']" /><span class="required-indicator">*</span> </div> <div style="display:none;" id="otherposition" class="fieldcontain ${haserrors(bean: dvdrequesterinstance, field: 'dvdrequesterotherposition', 'error')} required"> <label for="dvdrequesterotherposition" class="extended-label-width"> <g:message code="dvdrequester.dvdrequesterotherposition.label" /> </label> <g:textfield name="dvdrequesterotherposition" required="" value="${fieldvalue(bean:dvdrequesterinstance, field:'dvdrequesterotherposition')}" id="dvdrequesterotherposition" size="40" placeholder="other position" /><span class="required-indicator">*</span> </div> 3. jquery code in layout file <script> $(document).ready(function() { $('#position').on('change', function () { if (this.value == 'other') { $("#otherposition").show(); } else { $("#otherposition").hide(); } }); }); </script>
i got problem. not related grails instead of browser implementation. 1 may confused in problem.
basically, when hiding field using jquery, not removing dom , have marked field required
. when attempting submit form, validation fails on hidden field not getting displayed since have hidden , form not getting submitted.
(tip: if use chrome, message in javascript console.)
the fix problem simple also. have 2 options now:
the first option can remove required
attribute input field when hiding using jquery , can add attribute when displaying again.
the second option can permanenlty remove required
attribute , add small custom jquery validation validation field when field visible.
hope helps!
Comments
Post a Comment