From 535177025cb00f693538992b45a225a84edbad13 Mon Sep 17 00:00:00 2001
From: josebc <jose_bc@live.co.uk>
Date: Thu, 24 Jul 2014 13:20:50 +0300
Subject: [PATCH] better error messages

---
 cck_phone.module |   42 ++++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 20 deletions(-)

diff --git a/cck_phone.module b/cck_phone.module
index 3208d7b..7bc869d 100644
--- a/cck_phone.module
+++ b/cck_phone.module
@@ -480,6 +480,7 @@ function _cck_phone_validate(&$item, $delta, $field, $instance, $langcode, &$err
   if (isset($phone_input) && !empty($phone_input)) {
 
     $error_params = array(
+      '%name' => $instance['label'],
       '%phone_input' => check_plain($phone_input),   // original phone input
       '%countrycode' => check_plain($countrycode),
       '%min_length' => CCK_PHONE_PHONE_MIN_LENGTH,
@@ -490,9 +491,9 @@ function _cck_phone_validate(&$item, $delta, $field, $instance, $langcode, &$err
 
     // Only allow digit, dash, space and bracket
     if (!_cck_phone_valid_input($phone_input, $ext_input)) {
-      $error = t('The phone number must be between %min_length and %max_length digits in length.', $error_params);
+      $error = t('%name must be between %min_length and %max_length digits in length.', $error_params);
       if ($settings['enable_extension'] && $ext_input != '') {
-        $error .= '<br />' . t('The phone extension must be less than %ext_max_length digits in length.', $error_params);
+        $error .= '<br />' . t('%name extension must be less than %ext_max_length digits in length.', $error_params);
       }
 
       form_set_error($field['field_name'], $error);
@@ -500,15 +501,15 @@ function _cck_phone_validate(&$item, $delta, $field, $instance, $langcode, &$err
     else {
       if (!$settings['all_country_codes']) {
         if (!_cck_phone_valid_cc_input($settings['country_codes']['country_selection'], $countrycode)) {
-          $error = t('Invalid country code "%countrycode" submitted.', $error_params);
+          $error = t('%name: Invalid country code "%countrycode" submitted.', $error_params);
           form_set_error($field['field_name'], $error);
         }
       }
       // Generic number validation
       if (!cck_phone_validate_number($countrycode, $phone_input, $ext_input)) {
-        $error = t('The phone number must be between %min_length and %max_length digits in length.', $error_params);
+        $error = t('%name must be between %min_length and %max_length digits in length.', $error_params);
         if ($field['enable_extension'] && $ext_input != '') {
-          $error .= '<br />' . t('The phone extension must be less than %ext_max_length digits in length.', $error_params);
+          $error .= '<br />' . t('%name extension must be less than %ext_max_length digits in length.', $error_params);
         }
 
         form_set_error($field['field_name'], $error);
@@ -826,24 +827,25 @@ function cck_phone_phone_number_validate(&$element, &$form_state) {
   }
 
   if (isset($phone_input) && !empty($phone_input)) {
+    $error_params = array(
+      '%name' => $instance['label'],
+      '%phone_input' => check_plain($phone_input),   // original phone input
+      '%countrycode' => check_plain($countrycode),
+      '%min_length' => CCK_PHONE_PHONE_MIN_LENGTH,
+      '%max_length' => CCK_PHONE_PHONE_MAX_LENGTH,
+      '%ext_input' => check_plain($ext_input),
+      '%ext_max_length' => CCK_PHONE_EXTENSION_MAX_LENGTH,
+    );
+
     if (empty($countrycode)) {
-      form_set_error($field['field_name'], t('The phone number must be accompanied by a country code.'));
+      form_set_error($field['field_name'], t('%name must be accompanied by a country code.', $error_params));
     }
     else {
-      $error_params = array(
-        '%phone_input' => check_plain($phone_input),   // original phone input
-        '%countrycode' => check_plain($countrycode),
-        '%min_length' => CCK_PHONE_PHONE_MIN_LENGTH,
-        '%max_length' => CCK_PHONE_PHONE_MAX_LENGTH,
-        '%ext_input' => check_plain($ext_input),
-        '%ext_max_length' => CCK_PHONE_EXTENSION_MAX_LENGTH,
-      );
-
       // Only allow digit, dash, space and bracket
       if (!_cck_phone_valid_input($phone_input, $ext_input)) {
-        $error = t('The phone number must be between %min_length and %max_length digits in length.', $error_params);
+        $error = t('%name must be between %min_length and %max_length digits in length.', $error_params);
         if ($settings['enable_extension'] && $ext_input != '') {
-          $error .= '<br />' . t('The phone extension must be less than %ext_max_length digits in length.', $error_params);
+          $error .= '<br />' . t('%name extension must be less than %ext_max_length digits in length.', $error_params);
         }
 
         form_set_error($field['field_name'], $error);
@@ -851,15 +853,15 @@ function cck_phone_phone_number_validate(&$element, &$form_state) {
       else {
         if (!$settings['all_country_codes']) {
           if (!_cck_phone_valid_cc_input($settings['country_codes']['country_selection'], $countrycode)) {
-            $error = t('Invalid country code "%countrycode" submitted.', $error_params);
+            $error = t('%name: Invalid country code "%countrycode" submitted.', $error_params);
             form_set_error($field['field_name'], $error);
           }
         }
         // Generic number validation
         if (!cck_phone_validate_number($countrycode, $phone_input, $ext_input)) {
-          $error = t('The phone number must be between %min_length and %max_length digits in length.', $error_params);
+          $error = t('%name must be between %min_length and %max_length digits in length.', $error_params);
           if ($settings['enable_extension'] && $ext_input != '') {
-            $error .= '<br />' . t('The phone extension must be less than %ext_max_length digits in length.', $error_params);
+            $error .= '<br />' . t('%name extension must be less than %ext_max_length digits in length.', $error_params);
           }
 
           form_set_error($field['field_name'], $error);
-- 
1.7.9.5

