电子邮件在控制器Grails中进行验证 [英] Email Validation in a controller Grails

查看:153
本文介绍了电子邮件在控制器Grails中进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想验证控制器中的电子邮件地址,我认为这很简单。我所做的方法如下:

  def emailValidCheck(String emailAddress){
EmailValidator emailValidator = EmailValidator。 getInstance()
if(!emailAddress.isAllWhitespace()|| emailAddress!= null){
String [] email = emailAddress.replaceAll(// s +,)。split(, )
email.each {
if(emailValidator.isValid(it)){
return true
} else {return false}
}
}

$ / code>

这与sendMail函数一起使用,我的代码在这里:

  def emailTheAttendees(String email){
def user = lookupPerson()
if(!email .isEmpty()){
def splitEmails = email.replaceAll(// s +,)。split(,)
splitEmails.each {
def String currentEmail = it
sendMail {
到currentEmail
System.out.println(什么是解决:+ currentEmail)
主题您的朋友$ {user.username}邀请您作为任务参加者
html g.render(template:/ emails / Attendees)
}
}
}

}



<这可以工作,并发送电子邮件到有效的电子邮件地址,但如果我随意放入一些不是地址的东西,只会发送sendMail异常。我无法理解为什么它没有正确验证,甚至进入了正在save方法中调用的emailTheAttendees()方法。

解决方案

我建议使用约束一个命令对象来实现这一点。例子:

命令对象:

  @ grails.validation.Validateable 
class YourCommand {
字符串电子邮件
字符串otherStuffYouWantToValidate

静态约束= {
email(空白:false,电子邮件:true)
。 ..


在您的控制器中像这样调用它:

  class YourController {
def yourAction(YourCommand command){
if(command.hasErrors()){
//处理错误
返回
}

//使用命令对象数据
}
}
code>


I'm just trying to validate an email address in a Controller which I thought would be simple. The method I've done is as follows:

def emailValidCheck(String emailAddress)  {
    EmailValidator emailValidator = EmailValidator.getInstance()
    if (!emailAddress.isAllWhitespace() || emailAddress!=null) {
        String[] email = emailAddress.replaceAll("//s+","").split(",")
        email.each {
            if (emailValidator.isValid(it)) {
            return true
            }else {return false}
        }
    }
}

This is being used with a sendMail function, which my code for that is here:

def emailTheAttendees(String email) {
    def user = lookupPerson()
    if (!email.isEmpty()) {
        def splitEmails = email.replaceAll("//s+","").split(",")
        splitEmails.each {
            def String currentEmail = it
            sendMail {
                to currentEmail
                System.out.println("what's in to address:"+ currentEmail)
                subject "Your Friend ${user.username} has invited you as a task attendee"
                html g.render(template:"/emails/Attendees")
            }
        }
    }

}

This works and sends emails to valid email addresses, but if I put in something random that is not an address just breaks with sendMail exception. I can't understand why it's not validating correctly and even going into the emailTheAttendees() method ... which is being called in the save method.

解决方案

I would suggest using constraints and a command object to achieve this. Example:

Command Object:

@grails.validation.Validateable
class YourCommand {
    String email
    String otherStuffYouWantToValidate

    static constraints = {
        email(blank: false, email: true)
        ...
    }
}

Call it like this in your controller:

class YourController {
    def yourAction(YourCommand command) {
        if (command.hasErrors()) {
            // handle errors
            return
        }

        // work with the command object data
    }
}

这篇关于电子邮件在控制器Grails中进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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