2

我有一个用于用户注册的域类。在注册过程中,用户需要输入两次电子邮件,并且这两个需要匹配。我不想存储重新输入的电子邮件,只想将其用于验证。有什么建议吗?

我的域类看起来像:

class MyUser{

    String name
    String email
    Integer telephone
 }

我的观点如下:

<label for="reEmail" id="reEmail">
<g:message code="myUser.reEnter.label" default="Re-enter Email:" id="reEnter"/></label>
<g:textField name="reEmail"/>

提前致谢

4

4 回答 4

2

处理此问题的一个好方法是在您的类中使用瞬态字段。

像这样......

class MyUser{

    static constraints = {
      email (email: true, blank: false) //<-- just a good idea ;)
    }

    String name
    String email
    Integer telephone
    String emailAgain
    static transients = ['emailAgain'] //<-- won't be stored in the database

 }

然后,在您看来,您可以像处理其他任何领域一样处理该领域......

<g:textField name="email" />
<g:textField name="emailAgain" />

然后在你的控制器中验证它......

def save = {
   def myUser = new MyUser(params);
   if (myUser.email.equals(myUser.emailAgain)){ //<-- maybe make a helper method in the domain class (myUser.doesEmailMatchEmailAgain() ?)
     // do stuff
   }else{
     //handle error, tell user their email's don't match
   }
}
于 2012-05-11T15:19:38.167 回答
1

另一种选择是使用Command Objects。它们为您提供了让您的表单帖子作为一个可以具有约束和自定义验证的单个对象返回的好处(就像域类一样)。然后,一旦您对用户的输入感到满意,您就可以使用命令对象的值更新您的域对象。

当您有一种在几个地方提交的表单类型时,命令对象非常有用(认为用户注册和更新用户详细信息是两个表单相似的示例,因此您可以重用命令对象)。或者当您的页面上有更新多个域对象的表单或表单不能很好地 1:1 映射到域对象时。

不知道我解释得有多好,但他们很高兴知道。尽管它们在您的用例中可能有点矫枉过正。

此页面有一个示例两次输入密码并进行比较: http: //www.intelligrape.com/blog/2009/05/26/command-objects/

于 2012-05-12T02:36:37.343 回答
0

If email-confirmation is the case, I think you can forget about it completely at the server side. It never stops malicious attackers, so as you say, it isn't worth a place in your domain class. Re-entering only help mistaken users, so I think doing it by javascript at client side is good enough.

By that way, I think you can simply use a solution like jQuery validation plugin. Nothing grails-specific, I'm afraid.

You can also find a working demo here.

于 2012-05-15T07:26:37.930 回答
0

我已经做了几次不同的事情。第一个是在控制器中进行验证,但我清理了它并结束了在客户端的 jquery 中进行电子邮件验证。

这样,您就不必为不匹配的电子邮件来回走动。

这是一个用于检查电子邮件是否有效的示例 javascript 函数。现在您所要做的就是再次验证输入的第一个。

您只需要表单中的另一个字段参数用于第二封电子邮件。

// simple function to check if a email is of a valid email type
function IsEmail(email) {
  var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}

// method to check if email and confermation email is the same.

 $("#signupForm").validate({
rules: {
                ...snip... 
    email1: {
        required: true,
        minlength: 5
    },
    email2: {
        required: true,
        minlength: 5,
        equalTo: "#email1"
    },
               ...snip...
},
messages: {
    ...snip...
    email: {
        required: "Please provide a email",
        minlength: "Your email must be at least 5 characters long"
    },
    email2: {
        required: "Please provide a confermation email",
        minlength: "Your confermation email must be at least 5 characters long",
        equalTo: "Please enter the same email as above"
    },
    ...snip...
}

});

于 2012-05-11T15:21:29.293 回答