validationgrails-2.0grails-domain-classgrails-validation

Validate ip address in Grails


I am looking for a way to validate IP addresses in Grails via constraints.

Is something like this possible?

package example

class Ip {

    String ip

    static constraints = {
        ip(unique: true, inetAddress: true)
    }
}

I have found this link: http://grails.org/doc/2.2.x/api/org/codehaus/groovy/grails/validation/routines/InetAddressValidator.html, but I don't know how to implement this.


Solution

  • I found the solution I searched for

    import org.codehaus.groovy.grails.validation.routines.InetAddressValidator
    
    class Ip {
    
       String ip
    
    
    
     static constraints = {
        ip(blank: false, unique: true, validator: { 
             return InetAddressValidator.getInstance().isValidInet4Address(it) 
             } )
     }
    }