Skip to content

Instantly share code, notes, and snippets.

@arcesino
Last active May 3, 2017 16:39
Show Gist options
  • Save arcesino/5732227 to your computer and use it in GitHub Desktop.
Save arcesino/5732227 to your computer and use it in GitHub Desktop.
Grails: Split & bind comma separated string to List<String>
class SkuProfilesController {
def skuProfileService
def show() {
paramsSplit(['ids', 'fields'])
def cmd = bindData(new ListSkuProfilesCommand(), params)
if (!cmd.validate()) {
throw new EntityValidationException(cmd.errors)
}
restpond skuProfileService.findAllSkuProfilesByVerticalAndFields(cmd)
}
private void paramsSplit(List<String> splitableParams) {
splitableParams.each { splitableParam ->
def commaSeparatedString = params[splitableParam]?.toString() ?: ''
params[splitableParam] = commaSeparatedString.split(',').toList()
}
}
}
@Validateable
class ListSkuProfilesCommand {
Long verticalId
List<Long> ids
List<String> fields
static constraints = {
verticalId nullable: false
ids nullable: false, size: 1..20
}
}
@arcesino
Copy link
Author

arcesino commented Jun 7, 2013

Worth to mention that validation are still enforced without problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment