2

我有一个域约束,我正在验证它

userName(blank:false, nullable:false, Size: 5..50,matches:'^[A-Za-z\\d]*$',validator:{chkUser,user->if(user.loginService.getUser(user.organizationId,user.userName)!=null){
                                                                                                                    return[propertyName="userName"]}
                                                                                              })

所以我想做的是调用登录服务并查看用户名是否存在。如果用户名存在,我必须返回消息 USerName 已经存在。我已将我的 message.properties 修改为:

BuildUserNameCommand.userName.matches= Username Should have alphanumeric characters only
BuildUserNameCommand.userName.invalid.userName=Username already exists
BuildUserNameCommand.userName.validator.error= Username already exists

buildusernamecommand是我的班级名称。我也尝试return['invalid.userName']过约束。但它仍然没有显示自定义消息。

请帮帮我....

4

2 回答 2

3

If you only have Users as a domain objects, it's easier to use unique constraint:

username unique: true

Then, in your case, the message code to define would be buildUserNameCommand.userName.unique - and the constraint does it all for you. It will also generate DB schema uniqueness constraint.

于 2012-04-03T09:19:52.187 回答
3

您是否看到命令对象的任何其他自定义消息,例如匹配项?用户名是全球唯一的还是在组织范围内唯一的。如果全局唯一验证很容易使用 unique:true

如果只是在组织范围内唯一,您可以尝试从验证器返回字符串“alreadyExists”,然后以以下形式定义消息:

buildUserNameCommand.userName.alreadyExists = 'Username already exists'

如果这不起作用,另一种选择是从验证器返回 false。这可能会拉入消息

buildUserNameCommand.userName.validator.error= Username already exists

注意类名上的小写b 。不确定该类是否需要以小写字母开头,但在我的代码中它们需要,而且它似乎更符合 Grails 模式。

于 2012-04-03T08:20:32.687 回答