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); } }