2013-01-09 32 views
6

我想检查包含特定字符串的列表。Groovy/Grails包含小写字母

检查列表中的所有条目以及蜇应该在之前小写

我试过这样

def venueName = params.name 
def venueNameLists = Venue.executeQuery("select name from Venue") 
    if(venueNameLists.toLowerCase().contains(venueName.toLowerCase())){ 
      error = true; 
      log.debug("save :: duplicate name") 
      flash.message = "Venue name already exist"; 
      render(view: "create", model: [venueInstance: new Venue(params)]) 
      return 
     } 

给出错误

No signature of method: java.util.ArrayList.toLowerCase() is applicable for argument types:() values: []. Stacktrace follows: 

    groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.toLowerCase() is applicable for argument types:() values: [] 

回答

15

我同意艾俄罗斯:使用约束,或者尝试找到实例的名字忽略大小写。但要解决这个问题,请尝试*(star-dot)运营商:

venueNameLists*.toLowerCase().contains(venueName.toLowerCase()) 
6

如果您想检查在保存元素前重复输入,请在您的域类上使用constraints。在这里,您可以使用unique约束,或者在需要时使用您自己的case insensitive

如果您需要手动检查它,试试这个:

def venueWithNameFromParams = Venue.findByNameIlike(params.name) // ignore case 
if(venueWithNameFromParams){ 
    // venueName is in venueNameList 
} 
相关问题