-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from Bdaya-Dev/fix/phone-field
Fix reactive_phone_form_field
- Loading branch information
Showing
14 changed files
with
174 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 28 additions & 2 deletions
30
packages/reactive_phone_form_field/lib/src/validators/validation_message.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,30 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:reactive_phone_form_field/reactive_phone_form_field.dart'; | ||
|
||
class PhoneValidationMessage { | ||
static const String valid = 'phone.valid'; | ||
static const String validMobile = 'phone.validMobile'; | ||
static const String required = 'phone.required'; | ||
static const String invalidPhoneNumber = 'phone.invalidPhoneNumber'; | ||
static const String invalidCountry = 'phone.invalidCountry'; | ||
static const String invalidMobilePhoneNumber = | ||
'phone.invalidMobilePhoneNumber'; | ||
static const String invalidFixedLinePhoneNumber = | ||
'phone.invalidFixedLinePhoneNumber'; | ||
|
||
static Map<String, String Function(Object)> localizedValidationMessages( | ||
BuildContext context, | ||
) { | ||
final localizations = PhoneFieldLocalization.of(context); | ||
return { | ||
PhoneValidationMessage.required: (error) => | ||
localizations.requiredPhoneNumber, | ||
PhoneValidationMessage.invalidCountry: (error) => | ||
localizations.invalidCountry, | ||
PhoneValidationMessage.invalidFixedLinePhoneNumber: (error) => | ||
localizations.invalidFixedLinePhoneNumber, | ||
PhoneValidationMessage.invalidMobilePhoneNumber: (error) => | ||
localizations.invalidMobilePhoneNumber, | ||
PhoneValidationMessage.invalidPhoneNumber: (error) => | ||
localizations.invalidPhoneNumber, | ||
}; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/reactive_phone_form_field/lib/src/validators/validator/_exports.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export 'country_phone_validator.dart'; | ||
export 'required_validator.dart'; | ||
export 'valid_validator.dart'; |
24 changes: 24 additions & 0 deletions
24
packages/reactive_phone_form_field/lib/src/validators/validator/country_phone_validator.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:phone_form_field/phone_form_field.dart'; | ||
import 'package:reactive_forms/reactive_forms.dart'; | ||
import 'package:reactive_phone_form_field/reactive_phone_form_field.dart'; | ||
|
||
class CountryPhoneValidator extends Validator<PhoneNumber> { | ||
final Set<IsoCode> allowedCountries; | ||
|
||
const CountryPhoneValidator({ | ||
required this.allowedCountries, | ||
}); | ||
|
||
@override | ||
Map<String, Object>? validate(AbstractControl<PhoneNumber> control) { | ||
final value = control.value; | ||
|
||
if (value == null || value.nsn.trim().isEmpty) return null; | ||
|
||
if (!allowedCountries.contains(value.isoCode)) { | ||
return {PhoneValidationMessage.invalidCountry: allowedCountries}; | ||
} | ||
|
||
return null; | ||
} | ||
} |
Oops, something went wrong.