Skip to content

Commit c72134d

Browse files
committed
README.md
1 parent c994bbf commit c72134d

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Phone number constraint for Grails
2+
3+
[![Build](https://github.com/gpc/grails-phone-number-constraint/actions/workflows/build.yml/badge.svg)](https://github.com/gpc/grails-phone-number-constraint/actions/workflows/build.yml)
4+
5+
This plugin establishes a `phoneNumber` constraint property for validateable objects, that being domain objects, and
6+
objects implementing `grails.validation.Validateable`. It relies on
7+
Google's [libphonenumber](https://github.com/google/libphonenumber) Java implementation
8+
9+
## Installation
10+
11+
To use this plugin, add the plugin to `build.gradle`:
12+
13+
```groovy
14+
dependencies {
15+
compile "io.github.gpc:phone-number-constraint:1.0.0"
16+
}
17+
18+
```
19+
20+
## Example
21+
22+
Here is an example of a command object that uses the plugin:
23+
24+
```groovy
25+
class PhoneNumber implements Validateable {
26+
27+
String number
28+
29+
static constraints = {
30+
number nullable: false, phoneNumber: true
31+
}
32+
}
33+
```
34+
35+
## Configuration
36+
37+
There are three different ways to configure the constraint.
38+
39+
1. With a boolean, `true` or `false` enabling or disabling the constraint
40+
2. With a string, where the string is the two-letter code for `region`. If set, the constraint is enabled
41+
3. With a map object, for more configuration. If set, the constraint is enabled
42+
43+
### Configuration by map:
44+
45+
The following table describes the configuration options:
46+
47+
| Config key | default | description |
48+
|---------------|-----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
49+
| `region` | `US` | The default region for phone numbers without the international `+xxx` prefix |
50+
| `phoneFormat` | `INTERNATIONAL` | How phonenumbers are formatted, if `format` is true. Possible values to be found in [PhoneNumberFormat](https://github.com/google/libphonenumber/blob/a08d3711af51d79d1f9d7d3a12b2049da3a81e20/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L463) |
51+
| `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_ |
52+
53+
## PhoneNumberUtil
54+
55+
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.
56+
57+
## Unit-testing your constraints
58+
59+
When running a unit test, the cascade constraint isn't registered with Grails. To work around this issue, the test class
60+
must implement
61+
`org.grails.testing.GrailsUnitTest` and the following code must be added to the `setup()` method of the test:
62+
63+
```groovy
64+
class ParentSpec extends Specification implements GrailsUnitTest {
65+
66+
@Override
67+
Closure doWithSpring() {
68+
return {
69+
constraintEvaluator(DefaultConstraintEvaluator)
70+
}
71+
}
72+
73+
void setup() {
74+
PhoneNumberConstraintRegistration.register(applicationContext)
75+
}
76+
77+
}
78+
```
79+
80+
This will register the `PhoneNumberConstraint` the same way as the plugin does at runtime.

0 commit comments

Comments
 (0)