Use Laravel Go Phone to parse numbers, validate form input and store either E.164 or the original text on a model.
go get github.com/bowerbird-si/laravel-go-phone| Import suffix | Package | Use |
|---|---|---|
| Root | phone | Parse, check and format numbers |
/validation | phonevalidation | Validate a Laravel for Go form field |
/model | phonemodel | Store raw text or E.164 on a model |
Parse and format
Pass country hints for national input. The first matching hint is used. A number with an international calling code that can be parsed independently takes precedence.
n := phone.New("0470 12 34 56", "BE")
n.Valid() // true
n.Country() // "BE"
n.International() // "+32 470 12 34 56", nil
n.National() // "0470 12 34 56", nil
n.String() // "+32470123456"
Use phone.Parse when you need a parse error immediately. New keeps the original text so String can display it even when formatting fails. A parseable international number can still fail Valid. WithLeniency(true) returns a new value that checks possible lengths. A successful check confirms a numbering-plan pattern or possible length. Assignment and reachability require separate verification.
An empty country means the region could not be determined. Global services such as +800 numbers can have region 001.
Validate a field
form := struct {
Phone string `json:"phone"`
Country string `json:"phone_country"`
}{Phone: "0470 12 34 56", Country: "BE"}
errs, err := validation.Validate(ctx, &form,
rule.Field(&form.Phone, rule.Required, phonevalidation.Rule().Mobile()),
)
errs contains field failures. err contains errors that abort validation, such as a rule configured with both allowed and blocked types.
A nonempty sibling phone_country provides the first hint. Country supplies additional hints, or DefaultCountries supplies them when the rule has no explicit countries. These countries also act as an allowlist. Add International() to accept a valid international number outside that list while keeping the hints for national input.
Use Type, NotType, Mobile or FixedLine to restrict the number type. FixedLine and Mobile both match numbers whose plan cannot distinguish the two. Lenient() uses the possible-length check and keeps your country and type restrictions. Wrap the rule with rule.Message(check, message) to use an application-supplied message. Laravel skips a missing, blank or null value unless a required or other implicit rule applies.
Nested forms
Define the rule on the nested struct. Laravel then resolves each item's country within that item.
type ContactInput struct {
Phone string `json:"phone"`
Country string `json:"phone_country"`
}
func (c *ContactInput) Rules() validation.Rules {
return validation.For(c, rule.Field(&c.Phone, phonevalidation.Rule()))
}
Use CountryField("dialing_country") for another field on that form. Use RootCountryField("account.country") for a path from the request root. When validating a struct directly, pass any already-bound root country to the rule explicitly. Laravel provides a relative accessor for bound fields. Laravel for Go issue #429
Store numbers on a model
type Contact struct {
ID int64 `json:"id"`
Phone phonemodel.E164 `json:"phone"`
Raw *phonemodel.Raw `json:"raw"`
Country string `json:"country"`
}
canonical, err := phonemodel.NewE164(form.Phone, form.Country)
if err != nil {
return err
}
raw := phonemodel.Raw(form.Phone)
contact := Contact{Phone: canonical, Raw: &raw, Country: form.Country}
return model.Create(ctx, &contact)
Phone.Number() returns the parsed value. Raw.Number(contact.Country) resolves the raw text with the supplied country after the row is read. Check the raw pointer for nil before resolving it.
A zero E164 or nil *Raw writes SQL NULL and encodes as JSON null. An E164 write requires a region so that the resulting value can be read back. The constructor returns phone.ErrUnparseable for text it cannot parse, including a national number without a usable hint. It returns ErrUnknownRegion when a parseable value lacks a detectable region in E.164 form. The validation rule checks numbering-plan validity before you construct a model field. Use the raw field when retaining spaces, punctuation or a leading zero matters.