Skip to content

Commit

Permalink
Fix phone input (#201)
Browse files Browse the repository at this point in the history
* Fix phone input

* remove extra

* fix factory

* Fix phone input

* Validate phone number rule

* Prefill support for country only

* Fix phone input error

* fix tests

---------

Co-authored-by: Julien Nahum <[email protected]>
  • Loading branch information
formsdev and JhumanJ authored Sep 18, 2023
1 parent 08db014 commit d75975b
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 94 deletions.
2 changes: 1 addition & 1 deletion app/Http/Requests/AnswerFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
Expand Down
15 changes: 5 additions & 10 deletions app/Rules/ValidPhoneInputRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@ 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;

$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
return $phoneUtil->isValidNumber($phoneUtil->parse($value));
}

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.';
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.2",
"ext-json": "*",
"aws/aws-sdk-php": "^3.183",
"doctrine/dbal": "^3.4",
"fruitcake/laravel-cors": "^2.0",
"giggsey/libphonenumber-for-php": "^8.13",
"guzzlehttp/guzzle": "^7.0.1",
"jhumanj/laravel-model-stats": "^0.4.0",
"laravel/cashier": "^13.4",
Expand Down
Loading

0 comments on commit d75975b

Please sign in to comment.