# contact-forms/2.3.5/classes/Element/Telephone.php

Contact Forms by Cimatti, version 2.3.5. 101 lines.

- Page: https://pluginprobe.com/plugins/contact-forms/2.3.5/code/classes/Element/Telephone.php
- Raw: https://pluginprobe.com/plugins/contact-forms/2.3.5/raw/classes/Element/Telephone.php
- Modified: 2026-04-08T09:49:58+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/contact-forms/2.3.5/code/classes/Element/Telephone.php#L10-L20`.

```php
<?php
/**
 * Telephone Element for Contact Forms
 * 
 * Renders an accessible telephone input with proper HTML5 attributes.
 * Uses type="tel" for mobile keyboard optimization, proper autocomplete,
 * and integrates with the Phone validation class.
 * 
 * Data is stored as plain text in the database - no changes to existing data.
 * 
 * @package Contact Forms
 * @subpackage Element
 * @since 2.0.0-beta.20
 */

class AccuaForm_Element_Telephone extends Element_Textbox {
  
  /**
   * Country code for validation (ISO 3166-1 alpha-2)
   * 
   * @var string
   */
  protected $country_code = 'IT';
  
  /**
   * Constructor
   * 
   * Sets up telephone-specific HTML5 attributes for accessibility and UX:
   * - type="tel" for mobile numeric keyboard
   * - inputmode="tel" for better mobile support
   * - autocomplete="tel" for browser autofill
   * - data-country for libphonenumber validation
   * - Automatic phone validation
   * 
   * @param string $label The field label
   * @param string $name The field name attribute
   * @param array|null $properties Optional element properties. Supports 'country_code' for validation.
   */
  public function __construct($label, $name, ?array $properties = null) {
    // Extract country_code before parent constructor
    if (isset($properties['country_code'])) {
      $this->country_code = strtoupper(sanitize_text_field($properties['country_code']));
      unset($properties['country_code']); // Don't pass to parent
    }
    
    // Extract custom format message before parent constructor
    $custom_format_message = '';
    if (isset($properties['custom_format_message'])) {
      $custom_format_message = $properties['custom_format_message'];
      unset($properties['custom_format_message']);
    }
    
    parent::__construct($label, $name, $properties);
    
    // Set HTML5 telephone input type for mobile keyboard optimization
    $this->attributes['type'] = 'tel';
    
    // Input mode for better mobile keyboard support
    $this->attributes['inputmode'] = 'tel';
    
    // Autocomplete hint for browser autofill
    $this->attributes['autocomplete'] = 'tel';
    
    // Data attribute for libphonenumber-js validation (used by phone-validation.js)
    $this->attributes['data-country'] = esc_attr($this->country_code);
    
    // Add CSS class for styling and JS targeting
    $this->setClass('accuaform-telephone');
    
    // Attach phone validation with translated or custom error message
    if ($custom_format_message !== '') {
      $phone_error_msg = str_replace(array('%s', '%element%'), $label, $custom_format_message);
    } else {
      // translators: %element% is the field label.
      $phone_error_msg = str_replace('%element%', $label, __( "Please enter a valid phone number in %element% (e.g. +39 333 1234567)", 'contact-forms' ));
    }
    $this->setValidation(new AccuaForm_Validation_Phone($phone_error_msg));
  }
  
  /**
   * Override to normalize prefix-only values (e.g. "+39") to empty before validation.
   *
   * This ensures Required validation correctly treats prefix-only values as empty
   * (the user has not entered an actual phone number, only the default country prefix).
   *
   * @param string|null $value The raw POST value.
   * @return bool True if all validators pass.
   */
  public function isValid($value) {
    if (is_string($value) && $value !== '') {
      $trimmed = trim($value);
      $digitCount = strlen(preg_replace('/\D/', '', $trimmed));
      if (strpos($trimmed, '+') === 0 && $digitCount <= 4) {
        $value = '';
      }
    }
    return parent::isValid($value);
  }
  
}

```
