I have the following Command Object in my Grails 2.2.3 application.
@Validateable
class PictureCommand {
MultipartFile picture
String notes
Boolean isDocument
static constraints = {
picture nullable: false, validator: { val ->
if (!val.contentType in ['image/jpeg', 'image/png', 'image/pjpeg', 'image/gif','image/bmp'])
return 'fileType.invalid'
}
notes nullable: true
isDocument nullable: true
}
}
This allows for uploading a picture file, and ensures that it is in the list of valid types. The problem, however, is I can upload files that are NOT in that list. When I call cmd.validate()
I get true
, then I call cmd.hasErrors()
and get false
. I have tried returning the String 'fileType.invalid'
and the List ['fileType.invalid']
and neither work. Does anyone have any ideas? Thanks.
You need some parentheses to get the correct operator order:
if (!(val.contentType in ['image/jpeg', 'image/png', 'image/pjpeg', 'image/gif','image/bmp']))
Currently it's evaluating !val
first, and that's always true if it's not null or blank.