Fix phone input (#201)

* 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 <julien@nahum.net>
This commit is contained in:
formsdev
2023-09-18 18:42:05 +05:30
committed by GitHub
parent 08db014cde
commit d75975bdec
10 changed files with 257 additions and 95 deletions

View File

@@ -190,7 +190,7 @@ class AnswerFormRequest extends FormRequest
}
return $this->getRulesForDate($property);
case 'phone_number':
return [new ValidPhoneInputRule];
return ['string', 'min:6', new ValidPhoneInputRule];
default:
return [];
}

View File

@@ -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.';
}
}