This plugin establishes a phoneNumber
constraint property for validateable objects, that being domain objects, and
objects implementing grails.validation.Validateable
. It relies on
Google's libphonenumber Java implementation
To use this plugin, add the plugin to build.gradle
:
dependencies {
compile "io.github.gpc:phone-number-constraint:1.0.0"
}
Here is an example of a command object that uses the plugin:
class PhoneNumber implements Validateable {
String number
static constraints = {
number nullable: false, phoneNumber: true
}
}
There are three different ways to configure the constraint.
- With a boolean,
true
orfalse
enabling or disabling the constraint - With a string, where the string is the two-letter code for
region
. If set, the constraint is enabled - With a map object, for more configuration. If set, the constraint is enabled
The following table describes the configuration options:
Config key | default | description |
---|---|---|
region |
US |
The default region for phone numbers without the international +xxx prefix |
phoneFormat |
INTERNATIONAL |
How phonenumbers are formatted, if format is true. Possible values to be found in PhoneNumberFormat |
format |
false |
Will update the target object, with the property name with a formatted version of the phone number. If INTERNATIONAL is set in phoneFormat the international dialing code starting with +xxx is written to the field value. This is an experimental feature |
The class dk.glasius.phoneconstraint.PhoneNumberUtil
has convenience methods for validating, parsing and formatting a phone-number. This could be used for formatting in the UI of your application.
When running a unit test, the cascade constraint isn't registered with Grails. To work around this issue, the test class
must implement
org.grails.testing.GrailsUnitTest
and the following code must be added to the setup()
method of the test:
class ParentSpec extends Specification implements GrailsUnitTest {
@Override
Closure doWithSpring() {
return {
constraintEvaluator(DefaultConstraintEvaluator)
}
}
void setup() {
PhoneNumberConstraintRegistration.register(applicationContext)
}
}
This will register the PhoneNumberConstraint
the same way as the plugin does at runtime.