npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

phone-control

v1.0.0

Published

Client side script for Nette Forms Phone Control component

Downloads

2

Readme

Phone control: final solution for validating phone numbers in Nette forms

Introduction

InvolveDigital brings you complete solution for phone numbers in Nette forms. Features include:

  • validation via "giggsey/libphonenumber-for-php" library for backend and "libphonenumber-js" library for frontend
  • freedom of phone number format on input
  • unification of phone number format in output
  • complete region codes solution

Installation

The recommended way to install is via Composer:

composer require involve-digital/phone-control

Minimal required PHP is 7.1 and Nette 2.4

Client-side support can be installed with npm or yarn:

npm install phone-control

Usage

Backend

Config

Register extension and configure in configuration files.

  • in example you can see default configuration
  • you don't have to configure, if you dont need anything different
extensions:
        forms.phoneControl: Involve\Forms\Controls\PhoneControl\DI\PhoneControlExtension
        
forms.phoneControl:
    allowedRegions: [] # only allowed regions
    expectedRegions: [CZ, SK] # regions that we expect will be typed, but other regions are not restricted
    outputFormat: constant(libphonenumber\PhoneNumberFormat::E164) # output format
    outputFormatWhitespaces: true # set to false, if you want to trim whitespaces in output phone number
    regex: constant(PhoneControl::DEFAULT_REGEX) # first degree of protection, you can specify your own regex here

Let's have number +420 608 343 634 number as an example. Allowed values for outputFormat field are:

Nette form

$form->addPhone('phone', 'Phone:')
    ->setAllowedRegions('CZ');
//  ->setExpectedRegions(string|array $expectedRegions)
//  ->setOutputFormat(int $outputFormat) - see table above
//  ->setOutputFormatWhitespaces(bool $whitespaces)

Without client-side script

You can definitely use this extension server side only. The user will be forced to write region code though, so you might want to leave that information somewhere near the input.

Alternatively you can preset default region code. This component essentially adds two inputs into form; TextInput for actual phone number and HiddenInput for region code, mainly used by client side script. You could use the hidden input for setting default region code:

$form->addPhone('phone', 'Phone:')
    ->setAllowedRegions('CZ');

$form['phoneRegionCode']->setValue('+420'); // hidden input is created by adding postfix "RegionCode" to the name of the parent input

Frontend

True beauty comes with the client side script. Script is dependent on netteForms.js library. Usage of libphonenumber-js library is optional as you can write your own validation without this library.

How does it work?

  1. a dropdown is appended at the beginning of input
  2. dropdown contains specified countries/regions options
  3. on select, region code value is saved to hidden input
  4. on select of option "other", additional input for other region code is added
  5. on typed "+" sign in phone input, dropdown is hidden as it is pressumed, that user will enter region code number in main imput
<script src="netteForms.js"></script>
<script src="phone-control.js"></script>

Global object Nette is updated and Nette.PhoneControl object is created, where all functionality resides.

There are some options, that you can modify.

Config

<script>
    Nette.PhoneControl.options = {
        'flag': true, // shows country flags in dropdown
        'otherText': '-jiné-', // you might want to pass translated text here
        'singleOptionPaddingOffset': 15, // padding-left of input that dropdown is in
        'multiOptionsPaddingOffset': 20, // padding-left of input that dropdown is in
        'dropdownChevronTopOffset': 8 // top offset of dropdown chevron icon
    };
</script>

Useful functions

You can validate control via PhoneControl.validateControl function:

var valid = Nette.PhoneControl.validateControl(
  document.getElementById('test-phone-control')
);

You can also write your own validation:

Nette.PhoneControl.validateControl = function (phoneControl) {
    //... your validation
  };

Input is validated autmatically when Nette.validateForm() is called (e.g. on submit). If you don't want that for some reason, you can unset validator:

delete Nette.validators['InvolveFormsControlsPhoneControl_validateNumber'];

You might want to reinitialize phone control, espetially after snippet redrawal>

$.nette.ajax({
  url: '...',
  complete: function () {
    Nette.PhoneControl.initControl(
      document.getElementById('test-phone-control')
    );
  }
});

CSS

Current CSS is ready for Bootstrap framework, there is high probability, that you'll have to do some CSS adjustments in your projects for this component to work. You can also style the component from scratch. Please see .css and .scss file, copy contents to your project and modify as needed.

Most important styles to be modified are:

/* positioning of dropdown in input */
.phone-control-region-code {
    margin-top: 8px !important;
    margin-left: 10px !important;
}

/* hover over dropdown options */
.phone-control-region-code li:not(:first-child):hover {
    background-color: rgb(184, 233, 134) !important;
    color: white;
}

/* input for other region code */
.phone-control-region-code li.phone-control-selected input[type="tel"] {
    height: 16px !important;
    width: 30px !important;
}