package org.mindtrails.domain.forms.validation;


import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class PhoneValidator implements ConstraintValidator<Phone, HasPhone> {

    private Phone phone;

    @Override
    public void initialize(Phone phone) {
        this.phone = phone;
    }

    @Override
    public boolean isValid(HasPhone hasPhone, ConstraintValidatorContext ctx) {

        // Don't validate null phones.
        if (hasPhone.getPhone() == null) { return true; }

        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        try {
            Phonenumber.PhoneNumber usPhone = phoneUtil.parse(hasPhone.getPhone(), hasPhone.getPhoneLocale());
            return phoneUtil.isValidNumber(usPhone);
        } catch (NumberParseException e) {
            return false;
        }
    }
}
@@ -671,16 +671,45 @@
  == null
+ %7C%7C hasPhone.getPhone() == %22%22
 ) %7B retu

Allow empty phone numbers.