Integrate PhoneNumberValidator into SwishForm

PhoneNumberValidator + Test Changes:
  Added no-arg constuctor required for Guice injection.
  Added isoCountryCode setter;  this field must be set before
  validation.
  Added validation of isoCountryCode.
  Added missing country code test.

SwishValidator Changes:
  SwishValidator now extends PhoneValidator.
  Added no-arg constuctor required for Guice injection.
  Set isoCountryCode = SE in constructor.

SwishForm Changes:
  Sets Swish acct phone number to normalized phone number if
  phone # validation is successful.
  Replaced Logger declaration with @Slf4j annotation.
  Formatted source.

Added 'validation.phone.missingCountryCode' property to i18n file.
This commit is contained in:
ghubstan 2019-08-30 14:21:17 -03:00
parent 39d823a6fb
commit de4b59048b
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
5 changed files with 72 additions and 29 deletions

View File

@ -2984,6 +2984,7 @@ validation.mustBeDifferent=Your input must be different from the current value
validation.cannotBeChanged=Parameter cannot be changed
validation.numberFormatException=Number format exception {0}
validation.mustNotBeNegative=Input must not be negative
validation.phone.missingCountryCode=Need two letter country code to validate phone number
validation.phone.invalidCharacters=Phone number {0} contains invalid characters
validation.phone.insufficientDigits=Not enough digits in {0} for a valid phone number
validation.phone.tooManyDigits=Too many digits in {0} to be a valid phone number

View File

@ -35,20 +35,30 @@ import bisq.core.util.validation.InputValidator;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import static bisq.desktop.util.FormBuilder.addCompactTopLabelTextField;
import static bisq.desktop.util.FormBuilder.addCompactTopLabelTextFieldWithCopyIcon;
import static bisq.desktop.util.FormBuilder.addTopLabelTextField;
@Slf4j
public class SwishForm extends PaymentMethodForm {
private static final Logger log = LoggerFactory.getLogger(SwishForm.class);
private final SwishAccount swishAccount;
private final SwishValidator swishValidator;
private InputTextField mobileNrInputTextField;
public SwishForm(PaymentAccount paymentAccount,
AccountAgeWitnessService accountAgeWitnessService,
SwishValidator swishValidator,
InputValidator inputValidator,
GridPane gridPane,
int gridRow,
BSFormatter formatter) {
super(paymentAccount, accountAgeWitnessService, inputValidator, gridPane, gridRow, formatter);
this.swishAccount = (SwishAccount) paymentAccount;
this.swishValidator = swishValidator;
}
public static int addFormForBuyer(GridPane gridPane, int gridRow,
PaymentAccountPayload paymentAccountPayload) {
addCompactTopLabelTextFieldWithCopyIcon(gridPane, ++gridRow, Res.get("payment.account.owner"),
@ -58,13 +68,6 @@ public class SwishForm extends PaymentMethodForm {
return gridRow;
}
public SwishForm(PaymentAccount paymentAccount, AccountAgeWitnessService accountAgeWitnessService, SwishValidator swishValidator,
InputValidator inputValidator, GridPane gridPane, int gridRow, BSFormatter formatter) {
super(paymentAccount, accountAgeWitnessService, inputValidator, gridPane, gridRow, formatter);
this.swishAccount = (SwishAccount) paymentAccount;
this.swishValidator = swishValidator;
}
@Override
public void addFormForAddAccount() {
gridRowFrom = gridRow + 1;
@ -117,6 +120,9 @@ public class SwishForm extends PaymentMethodForm {
@Override
public void updateAllInputsValid() {
if (swishValidator.validate(swishAccount.getMobileNr()).isValid) {
swishAccount.setMobileNr(swishValidator.getNormalizedPhoneNumber());
}
allInputsValid.set(isAccountNameValid()
&& swishValidator.validate(swishAccount.getMobileNr()).isValid
&& inputValidator.validate(swishAccount.getHolderName()).isValid

View File

@ -23,17 +23,17 @@ public class PhoneNumberValidator extends InputValidator {
/**
* ISO 3166-1 alpha-2 country code
*/
private final String isoCountryCode;
private String isoCountryCode;
/**
* The international calling code mapped to the 'isoCountryCode' constructor argument.
*/
@Nullable
@Getter
private final String callingCode;
private String callingCode;
/**
* The normalized (digits only) representation of an international calling code.
*/
private final String normalizedCallingCode;
private String normalizedCallingCode;
/**
* Phone number in E.164 format.
*/
@ -41,23 +41,33 @@ public class PhoneNumberValidator extends InputValidator {
@Getter
private String normalizedPhoneNumber;
// Hide no-arg constructor and set final String fields
private PhoneNumberValidator() {
this.isoCountryCode = null;
this.callingCode = null;
this.normalizedCallingCode = null;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Constructors
///////////////////////////////////////////////////////////////////////////////////////////
// Public no-arg constructor required by Guice injector,
// but isoCountryCode must be set before validation.
public PhoneNumberValidator() {
}
public PhoneNumberValidator(String isoCountryCode) {
this.isoCountryCode = isoCountryCode;
this.callingCode = CountryCallingCodes.getCallingCode(isoCountryCode);
this.normalizedCallingCode = CountryCallingCodes.getNormalizedCallingCode(isoCountryCode);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Public methods
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public ValidationResult validate(String input) {
normalizedPhoneNumber = null;
ValidationResult result = super.validate(input);
ValidationResult result = super.validate(isoCountryCode);
if (!result.isValid) {
return new ValidationResult(false, Res.get("validation.phone.missingCountryCode"));
}
result = super.validate(input);
if (!result.isValid) {
return result;
}
@ -89,6 +99,21 @@ public class PhoneNumberValidator extends InputValidator {
return result;
}
/**
* Setter for property 'isoCountryCode'.
*
* @param isoCountryCode Value to set for property 'isoCountryCode'.
*/
public void setIsoCountryCode(String isoCountryCode) {
this.isoCountryCode = isoCountryCode;
this.callingCode = CountryCallingCodes.getCallingCode(isoCountryCode);
this.normalizedCallingCode = CountryCallingCodes.getNormalizedCallingCode(isoCountryCode);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Private methods
///////////////////////////////////////////////////////////////////////////////////////////
private ValidationResult validateIsNumeric(String rawInput, String pureNumber) {
try {
if (isPositiveNumber(pureNumber)) {

View File

@ -17,9 +17,17 @@
package bisq.desktop.util.validation;
import bisq.core.util.validation.InputValidator;
public final class SwishValidator extends PhoneNumberValidator {
public final class SwishValidator extends InputValidator {
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
// Public no-arg constructor required by Guice injector.
// Superclass' isoCountryCode must be set before validation.
public SwishValidator() {
this.setIsoCountryCode("SE");
}
///////////////////////////////////////////////////////////////////////////////////////////
// Public methods
@ -27,11 +35,6 @@ public final class SwishValidator extends InputValidator {
@Override
public ValidationResult validate(String input) {
// TODO
return super.validate(input);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Private methods
///////////////////////////////////////////////////////////////////////////////////////////
}

View File

@ -20,6 +20,15 @@ public class PhoneNumberValidatorTest {
Res.setup();
}
@Test
public void testMissingCountryCode() {
validator = new PhoneNumberValidator();
validationResult = validator.validate("+12124567890");
assertFalse("Should not be valid if validator's country code is missing", validationResult.isValid);
assertEquals(Res.get("validation.phone.missingCountryCode"), validationResult.errorMessage);
assertNull(validator.getNormalizedPhoneNumber());
}
@Test
public void testNoInput() {
validator = new PhoneNumberValidator("AT");
@ -266,7 +275,6 @@ public class PhoneNumberValidatorTest {
assertNull(validator.getNormalizedPhoneNumber());
}
@Test
public void testUSAreaCodeMatchesCallingCode() {
// These are not valid US numbers because these area codes