From 63552082264d6ae79a326e643080611e88b14b51 Mon Sep 17 00:00:00 2001 From: Forms Dev Date: Fri, 15 Sep 2023 15:20:02 +0530 Subject: [PATCH 1/8] Fix phone input --- app/Http/Requests/AnswerFormRequest.php | 2 +- app/Rules/ValidPhoneInputRule.php | 13 +--- package.json | 1 + resources/js/components/forms/PhoneInput.vue | 64 +++++++++++-------- .../forms/fields/components/FieldOptions.vue | 9 ++- 5 files changed, 50 insertions(+), 39 deletions(-) diff --git a/app/Http/Requests/AnswerFormRequest.php b/app/Http/Requests/AnswerFormRequest.php index 59617da96..d7c02441c 100644 --- a/app/Http/Requests/AnswerFormRequest.php +++ b/app/Http/Requests/AnswerFormRequest.php @@ -190,7 +190,7 @@ private function getPropertyRules($property): array } return $this->getRulesForDate($property); case 'phone_number': - return [new ValidPhoneInputRule]; + return ['string', 'min:6', new ValidPhoneInputRule]; default: return []; } diff --git a/app/Rules/ValidPhoneInputRule.php b/app/Rules/ValidPhoneInputRule.php index 612bd9cf0..23ed99fcc 100644 --- a/app/Rules/ValidPhoneInputRule.php +++ b/app/Rules/ValidPhoneInputRule.php @@ -9,21 +9,14 @@ class ValidPhoneInputRule implements Rule { public function passes($attribute, $value) { - if (!is_string($value)) { + if (!is_string($value) || !Str::startsWith($value, '+')) { return false; } - if (!Str::startsWith($value, '+')) { - return false; - } - $parts = explode(' ', $value); - if (count($parts) < 2) { - return false; - } - return strlen($parts[1]) >= 5; + return true; } public function message() { - return 'The :attribute must be a string that starts with a "+" character and must be at least 5 digits long.'; + return 'The :attribute is invalid.'; } } \ No newline at end of file diff --git a/package.json b/package.json index 861bd87fd..07da23d2b 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "fuse.js": "^6.4.6", "js-cookie": "^2.2.1", "js-sha256": "^0.9.0", + "libphonenumber-js": "^1.10.44", "portal-vue": "^2.1.7", "prismjs": "^1.24.1", "qrcode": "^1.5.1", diff --git a/resources/js/components/forms/PhoneInput.vue b/resources/js/components/forms/PhoneInput.vue index c189032b1..27c443844 100644 --- a/resources/js/components/forms/PhoneInput.vue +++ b/resources/js/components/forms/PhoneInput.vue @@ -12,10 +12,10 @@ -
- +
+ -
+
+ + + +
+
@@ -42,13 +48,11 @@ import {directive as onClickaway} from 'vue-clickaway' import inputMixin from '~/mixins/forms/input.js' import countryCodes from '../../../data/country_codes.json' import CountryFlag from 'vue-country-flag' -import VSelect from './components/VSelect.vue' +import parsePhoneNumber from 'libphonenumber-js' export default { phone: 'PhoneInput', - components: { - CountryFlag, VSelect - }, + components: { CountryFlag }, directives: { onClickaway: onClickaway }, @@ -56,18 +60,30 @@ export default { data() { return { - selectedCountryCode: countryCodes[234], + selectedCountryCode: this.getCountryByCode('US'), // Default US countries: countryCodes, - isOpen: false, - inputVal: '' + inputVal: null } }, + + mounted () { + if(this.compVal) { + const phoneObj = parsePhoneNumber(this.compVal) + if(phoneObj){ + this.selectedCountryCode = this.getCountryByCode(phoneObj.country) + this.inputVal = phoneObj.nationalNumber + } + } + }, + watch: { - inputVal(newVal, oldVal) { - if (newVal.startsWith('0')) { - newVal = newVal.replace(/^0+/, '') + inputVal: { + handler(val) { + if (val && val.startsWith('0')) { + val = val.substring(1) + } + this.compVal = (val) ? this.selectedCountryCode.dial_code + val : null } - this.compVal = this.selectedCountryCode.dial_code + ' ' + newVal }, selectedCountryCode(newVal, oldVal) { if (this.compVal) { @@ -76,18 +92,14 @@ export default { } }, methods: { - onCountryChange(country) { - this.selectedCountryCode = country - this.closeDropdown() - }, - closeDropdown() { - this.isOpen = false + getCountryByCode(code) { + return countryCodes.find((item) => { + return item.code === code + }) }, onInput(event) { - const input = event.target.value - const digitsOnly = input.replace(/[^0-9]/g, '') - this.inputVal = digitsOnly - }, + this.inputVal = event.target.value.replace(/[^0-9]/g, '') + } } } diff --git a/resources/js/components/open/forms/fields/components/FieldOptions.vue b/resources/js/components/open/forms/fields/components/FieldOptions.vue index 20957ff0a..7af6271fb 100644 --- a/resources/js/components/open/forms/fields/components/FieldOptions.vue +++ b/resources/js/components/open/forms/fields/components/FieldOptions.vue @@ -216,6 +216,11 @@ :date-range="field.date_range===true" label="Pre-filled value" /> + -