Files
opnform-host-nginx/app/Rules/ValidPhoneInputRule.php
formsdev f7ecd6f233 Phone input prefill country issue fixed (#213)
* fix prefill phone issue

* js phone parse fixes

* revert last change

* fix phone UI

* Code optimize

---------

Co-authored-by: Julien Nahum <julien@nahum.net>
2023-10-03 17:50:46 +02:00

44 lines
1.6 KiB
PHP

<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Str;
class ValidPhoneInputRule implements Rule
{
public ?int $reason = 0;
public function passes($attribute, $value)
{
if (!is_string($value) || !$value) {
return false;
}
try {
if(ctype_alpha(substr($value, 0, 2))){ // First 2 will be country code
$value = substr($value, 2);
}
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$phone = $phoneUtil->parse($value);
$this->reason = $phoneUtil->isPossibleNumberWithReason($phone);
return $this->reason === \libphonenumber\ValidationResult::IS_POSSIBLE;
} catch (\Exception $e) {
return false;
}
}
public function message()
{
return match ($this->reason) {
\libphonenumber\ValidationResult::IS_POSSIBLE => 'The :attribute is not valid for an unknown reason.',
\libphonenumber\ValidationResult::INVALID_COUNTRY_CODE => 'The :attribute does not have a valid country code.',
\libphonenumber\ValidationResult::TOO_SHORT => 'The :attribute is too short.',
\libphonenumber\ValidationResult::TOO_LONG => 'The :attribute is too long.',
\libphonenumber\ValidationResult::IS_POSSIBLE_LOCAL_ONLY => 'The :attribute is not a valid phone number (local number).',
\libphonenumber\ValidationResult::INVALID_LENGTH => 'The :attribute does not have a valid length.',
default => 'The :attribute is not a valid phone number.',
};
}
}