Search code examples
javascriptgrailsautocompletegsprequired

How to make a Grails richui g:autocomplete field mandatory


I can't see any way to make a richui autocomplete in grails mandatory. With a g:select I'd just use required="required" but this doesn't appear to work for g:autocompletes, any suggestions?

Currently my autocomplete looks like so...

<richui:autoComplete required="required" style="width:500px" name="autoLook[X].id" id="autoLook[X]." value="" action="${createLinkTo('dir': 'object/searchAJAX')}" forceSelection = "true" maxResultsDisplayed="20" minQueryLength ="3" onItemSelect="updateHiddenInput(id,X)" />

Is the only way of doing this going to be running a javascript check before submitting?


Solution

  • You can add required="required" when page is ready, like:

    Add class required in the richui:autoComplete

    <richui:autoComplete class="required" name="name[${idx}]" id="uniqueId[${idx}]" action="${createLinkTo('dir': 'oauthCallBack/test')}" onItemSelect="updateHiddenInput(id, this)"/>
    

    and script

    <script type="text/javascript">
        $(function () {
            $('.required').each(function (index) {
                jQuery(this).attr('required', 'required')
            });
        });
    </script>