Grails中不区分大小写的唯一约束 [英] Case-insensitive unique constraint in Grails

查看:122
本文介绍了Grails中不区分大小写的唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class User {$ b $}我如何基本上对字符串数据类型字段执行唯一约束。 b字符串用户名
字符串电子邮件

static hasMany = [角色:角色]

静态约束= {
电子邮件(email:true)
用户名(唯一:true)

}
}

是否有任何简单的方法来实现 username(unique:true)



或者我必须使用方法手动检查数据库例如 .findByNameLike



用户名应该是唯一的,但唯一性应该不区分大小写。

解决方案


简单的一个:


  • 将它们存储为大写或小写并使用唯一约束。



或者关于表现,更贵:


  • 以混合大小写形式存储它们并使用自定义验证器,它通过比较给定和现有的用户名不区分大小写来检查数据库。 现在,如果你只是想让用户自由地输入他的用户名(如果他想要的话)(第一种可能性),或者你想保留用户名的情况下显示的原因(第二种可能性)。



    你的问题听起来像第二个问题,所以自定义验证器看起来像这样:

      class User {
    字符串用户名
    字符串电子邮件
    $ b $静态hasMany = [角色:角色]
    静态约束= {
    email(email:true)
    username验证器:{
    return!User.findByUsernameILike(it)
    })
    }
    }

    希望有所帮助。

    Heinrich在他的评论中表示,当用户能够更改他们的用户名时,上面的验证器会导致问题。



    Quick&肮脏,但我认为这解决了这个问题:

      username(验证者:{val,obj  - > 
    def similarUser = User.findByUsernameILike(val)
    return!similarUser || obj.id == similarUser.id
    })

    请注意,它没有经过测试,我不确定您是否可以在验证器中定义变量。



    Meta:I永远不会让用户更改他们的用户名;)


    How can I basically carry out a unique constraint on a string data-type field.

    class User{
      String username
      String Email
    
      static hasMany = [roles:Roles]
    
         static constraints = {
         Email(email:true)
         username(unique:true)
    
        }
    }
    

    Is there any simple way to implement username(unique: true)

    Or must I manually check the database using methods like .findByNameLike?

    The username should be unique, but the uniqueness is should be case-insensitive.

    解决方案

    So, if you want to have unique and case insensitive usernames, there are two possible approaches.

    The simple one:

    • Store them upper or lower case and use the unique constraint.

    or, regarding performance, more expensive:

    • Store them in mixed case and use a custom validator, which checks the database by comparing given and existing usernames case insensitive.

    Now it depends, if you just want to give the user the freedom to enter his username in the case he wants (first possibility) or you want to keep the usernames case for displaying reasons (second possibility).

    Your question sounds like the second one, so a custom validator would look like this:

    class User { 
      String username 
      String email
    
      static hasMany = [roles:Roles]
      static constraints = {
        email(email:true)
        username(validator: {
                  return !User.findByUsernameILike(it)
                })
      }
    }
    

    Hope that helps.

    [Edit]

    As Heinrich states in his comment, the validator above will cause problems when users are able to change their username.

    Quick & dirty, but I think this solves the issue:

    username(validator: { val, obj ->
                          def similarUser = User.findByUsernameILike(val) 
                          return !similarUser || obj.id == similarUser.id
                        })
    

    Beware, it's not tested and I'm not sure if you're able to define variables in validators.

    Meta: I would never let the users change their username ;)

    这篇关于Grails中不区分大小写的唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆