1

在我的 User.groovy 域类中,我添加了我想在注册时捕获的字段字符串电子邮件。我定义了这些约束:

email email:true, blank: false, maxsize: 255, unique: true

当我注册时,如果我尝试重用现有的用户名,我会在用户名字段旁边收到一个很好的错误,说明用户名已被使用。我在生成的 HTML 中看到它来自

<span class='s2ui_error'>The username is taken</span>

但是,当我尝试重用现有电子邮件地址时,屏幕右上角会出现一个带有黄色文本的黑色窗格,上面写着:

“抱歉,处理您的注册时出现问题”。

仅当没有其他错误时才会出现此消息。

我想导致重复电子邮件错误生成类似于重复用户名错误的字段错误。我似乎无法找到这些附加标签的生成位置。

提前感谢您的任何建议。

4

1 回答 1

0

经过大量的试验和错误,我把所学的东西“回到了绘图板上”。我决定最好的结果是简单地通过用我自己的一个包含重复电子邮件检查的“附带”Spring Security UI 的默认 RegisterCommand.groovy 来完成这项工作。

我还决定摆脱 s2ui-override 进程提供的存根 RegisterController.groovy,以防万一受到干扰。

这种方法有效。新RegisterCommand.groovy的进去

src/main/groovy/grails/plugin/springsecurity/ui

在项目目录中,它看起来像这样:

/* Copyright 2015-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package grails.plugin.springsecurity.ui


class RegisterCommand implements CommandObject, grails.validation.Validateable {

    protected static Class<?> User
    protected static String usernamePropertyName

    String username
    String email
    String password
    String password2

    static constraints = {
        username validator: { value, command ->
            if (!value) {
                return
            }

            if (User.findWhere((usernamePropertyName): value)) {
                return 'registerCommand.username.unique'
            }
        }
        // Original begins
        // email email: true
        // Original ends
        // Modifications begin
        email email: true, validator: { value, command ->
            if (!value) {
                return
            }

            if (User.findWhere('email': value)) {
                return 'registerCommand.email.unique'
            }
        }
        // Modifications end
        password validator: RegisterController.passwordValidator
        password2 nullable: true, validator: RegisterController.password2Validator
    }
}

有两点值得评论:

  1. 不确定在顶部包含许可证文本的协议,但我认为我所做的是正确的;
  2. 有可能我可以在顶部声明一个静态 String emailPropertyName 而不是使用硬编码的键(并且依赖于其他一些代码来实现正确的值),但现在这似乎是最直接的解决方案。

一个评论可能不起作用 - 这么多年来,这个美妙的 Spring Security UI 已经可用,为什么还没有发现和记录呢?

于 2021-01-14T03:37:47.327 回答