577

Product support

Get help in the plugin support forum.

Categories

Outputs a form field to request the user's telephone number. Also includes a validation rule to validate a phone number with Google's libphonenumber. Both can be used independently.

Instructions

Install this plugin and add the "Telephone input field" component to any of your pages where you want the form element to be rendered.

Configuration

This plugin uses jackocnr/intl-tel-input and all options are supported (except customPlaceholder). A description of what each configuration option does can be found by hovering your mouse over the "i" icon on the drop down.

Phone number validator

The 'valid_phone_number' validation rule has also been included to validate the phone number on the server. Use it like any other validation rule. For example:

$data = Input::all();
$rules = [
    // ... Put your other validation rules here
    'phone' => [
        'bail',
        'required',
        'valid_phone_number',
    ],
];
$validation = Validator::make($data, $rules);

Phone max rate validator with CallWithUs

I'm basically using this for my own purposes, but if it's helpful to anyone else, great! This validator allows you to make sure the rate to call to the number using CallWithUs does not exceed a certain amount. This could be useful, for example, if you provide an instant "call me back" function that will automatically connect the user's phone with your business and want to make sure that the user is not putting in a number that will be too expensive for you.

First, you need to go to https://www.callwithus.com/ and sign up for an account. After you log in, you should see an "API key" on the main "Account Info" page.

Next, open your .env file in the root directory of your OctoberCMS installation. Note, this file name begins with a dot. Append the following to the bottom and substitute the value for the API key from the CallWithUs website:

CALLWITHUS_API_KEY=your_key_goes_here

Then, when you want to do the validation, you need to use the class:

use Multiwebinc\Internationaltelephoneinput\Validators\PhoneNumberMaxRateValidator;

And in the validation rules just do:

$rules = [
    // ... Put your other validation rules here
    'phone' => [
        'bail',
        'required',
        'valid_phone_number',
        new PhoneNumberMaxRateValidator(0.15, "Add your custom failure message here, or omit to use the default message"),
    ],
];

The above example will cause the validator to fail if the rate is greater than $0.15 USD per minute.

Instant callback example

If you wanted to use CallWithUs to provide an instant callback for your clients, this is what I'm doing:

First, in your HTML, use the onCallMeBack handler. For example:

<form data-request="{{ __SELF__ }}::onCallMeBack">

Then in your component, do:

public function onCallMeBack()
{
    $this->validateInput();

    $this->sendCallMeBackPostRequest();

    return Redirect::to($this->controller->pageUrl('contact-call-me-back-success'));
}

protected function sendCallMeBackPostRequest()
{
    $client = new \GuzzleHttp\Client();
    $response = $client->request('POST', 'https://www.callwithus.com/api/callback/', [
        'form_params' => [
            'key' => env('CALLWITHUS_API_KEY'),
            'called' => Input::get('phone'),
            'calling' => ____YOUR_PHONE_NUMBER_GOES_HERE____,
            'timeout' => '30'
        ],
    ]);

    $this->validateCallMeBackResponse(trim($response->getBody()));
}

protected function validateCallMeBackResponse($response)
{
    switch ($response) {
        case -1000: // invalid API key
        case -1001: // called number is not specified in the request
        case -1002: // called number is not specified in the request
        case -1005: // callback is blocked because you have low balance on your account
            $error = "An unknown error occurred";
            break;
        case -1003:
            $error = "Number is invalid";
            break;
        case -1004:
            $error = "Call failed or you did not answer. Please double check the number and try again.";
            break;
    }

    if (isset($error)) {
        throw new ValidationException(['phone' => $error]);
    }
}

Note: I am in no way affiliated with CallWithUs. I just use them for this service and thought it might be helpful for someone.

1.0.1

First version of internationaltelephoneinput

Mar 18, 2020