Search code examples
grailsgsp

Grails - multiple checkboxes to delimited string


I have found that people have tried and failed to bind multiple checkboxes to a single field. Here is my example.. could there be a more groovy way to do this?

index.gsp

     <html>
 <head>
<meta name="layout" content="main"/>

<title>Dat multiple check box to string tho</title>
 </head>
 <body>

<g:form action="submitFormIndex">
    Applicant Name<g:textField name="name" required="" value=""/>
    <br>


    Albany: <g:checkBox name="albany" value="albany" checked="false"  />  <br> 
    Allegany: <g:checkBox name="allegany" value="allegany" checked="false"  />  <br> 
    Bronx: <g:checkBox name="bronx" value="bronx" checked="false"  />  <br> 
    Broome: <g:checkBox name="broome" value="broome" checked="false"  />  <br> 
    Cattaraugus: <g:checkBox name="cattaraugus" value="cattaraugus" checked="false"  />  <br> 
    Cayuga: <g:checkBox name="cayuga" value="cayuga" checked="false"  />  <br> 
    Chautauqua: <g:checkBox name="chautauqua" value="chautauqua" checked="false"  />  <br> 
    Chemung: <g:checkBox name="chemung" value="chemung" checked="false"  />  <br> 
    Chenango: <g:checkBox name="chenango" value="chenango" checked="false"  />  <br> 
    Clinton: <g:checkBox name="clinton" value="clinton" checked="false"  />  <br>       
    Onondaga: <g:checkBox name="onondaga" value="onondaga" checked="false" /> <br> <br>

    <g:submitButton name="Submit"/>
</g:form>
 </body>
 </html>

TestFormController.groovy

 package countiesselectme

 class TestFormController {

def index() {}

def submitFormIndex(String name, String albany, String allegany, String bronx, String broome, String cattaraugus, String cayuga, String chautauqua, String chemung, String chenango, String clinton, String onondaga) {
    def cleanDemNulls = (["${albany}", "${allegany}", "${bronx}", "${broome}", "${cattaraugus}", "${cayuga}", "${chautauqua}", "${chemung}", "${chenango}", "${clinton}", "${onondaga}"]- 'null')
    new Applicant(name:"${name}" , countiesWillingToWork:cleanDemNulls).save()

    redirect (controller: "Applicant" , action:'index')
 }  


 }

Solution

  • You can use a command object or the params object to retrieve the checkbox values.

    GSP

    %{-- Value can be some gsp boolean var, this example uses constants --}%
    <g:checkbox name="albany" value="${true}"/>
    <g:checkbox name="allegany" value="${false}"/>
    

    Command object

    @Validateable
    def TestFormCommand {
        Boolean albany
        Boolean allegany
        ...
    
        def countiesList(){
            def counties = []
            if(albany) counties << "albany"
            if(allegany) counties << "aalleganybany"
            ...
            return counties
        }
    }
    

    Controller

    def action(TestFormCommand cmd){
        // Using command object
        def albany = cmd.albany
        def allegany = cmd.allegany
    
        // Using params
        def albanyParam = params.boolean('albany');
        def alleganyParam = params.boolean('allegany');
    
        // Use the utility method in the command object
        def countiesWillingToWork = cmd.countiesList
        ...
    }
    

    This should also mean you don't need to remove the null values as in your current code.

    As Giannis says in their comment, a multi-select might be better for this sort of data.

    <g:select from="${counties}" name="counties" multiple="${true}"/>
    

    You can then bind this in your command object with Set<String> counties or pull it from your params using params.list('counties').