I have a phone number field in my register form. I just realized that when the phone number field is empty, the registration does not proceed but no error message is printed. It confuses the user who does not know exactly what is wrong.
I did the following modifications to cck_phone.module. It might not be the best solution though. Sorry for the indents. Hate drupal's coding format.

function _cck_phone_validate(&$item, $delta, $field, $instance, $langcode, &$errors)
{
if (isset($item['number'])) {
$phone_input = trim($item['number']);
}
if (isset($item['country_codes'])) {
$countrycode = trim($item['country_codes']);
}
$ext_input = '';
$settings = $instance['settings'];

if ($settings['enable_extension']) {
$ext_input = trim($item['extension']);
}

if (isset($phone_input))
{
if (empty($phone_input))
{
$error = t('Phone number field is required.');
form_set_error($field['field_name'], $error);
}
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);
if ($settings['enable_extension'] && $ext_input != '') {
$error .= '
' . t('The phone extension must be less than %ext_max_length digits in length.', $error_params);
}

form_set_error($field['field_name'], $error);
}
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);
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);
if ($field['enable_extension'] && $ext_input != '') {
$error .= '
' . t('The phone extension must be less than %ext_max_length digits in length.', $error_params);
}

form_set_error($field['field_name'], $error);
}
// Country level validation if enabled
elseif ($settings['enable_country_level_validation']) {
$custom_cc = _cck_phone_custom_cc();

if (isset($custom_cc[$countrycode])) {
$validate_function = $countrycode . '_validate_number';

if (function_exists($validate_function)) {
$error = '';
if (!$validate_function($phone_input, $ext_input, $error)) {
form_set_error($field['field_name'], t($error, $error_params));
}
}
}
}
}
}
}
}

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

lord_of_freaks’s picture

Status: Active » Needs review
FileSize
607 bytes

Hi

The code there looks like old, but in the latest version of 7.x-1.x i´ve found that the problem with this is that the #title property is not set.

Attached there is a patch to solve it.

Hope it helps.

a.hjelm’s picture

Hi,

Noticed this too, I just made a quick update to the patch so that it will output the field title instead of simply "Number" on errors.

a.hjelm’s picture

Well that was a mistake, was too quick and didn't check the code before making changes.

I have now implemented a proper validate function that won't allow required fields to be empty and displays a proper error message without affecting anything else.

josebc’s picture

could not apply patch, could you please check patch

RayCascella’s picture

The easiest patch fix is to update the commented out '#title' code to the following:

'#title' => $element['#title'],
'#title_display' => 'invisible',

All of the error displaying is already handled by the core field processor. So, no other changes are needed.

However, you could also add this code to a custom module, to set those same values, without patching the module, if you prefer not to apply patches, and control everything in your own modules.

function custom_field_widget_form_alter(&$element, &$form_state, &$context){
  if($element['#type'] == 'phone_number'){
    $element['#process'][] = '_custom_process_phone_field';
  }
}

function _custom_process_phone_field(&$element, &$form_state, $form){
  $element['number']['#title'] = $element['#title'];
  $element['number']['#title_display'] = 'invisible';
  return $element;
}
PaulDinelle’s picture

Made a patch with RayCascella's suggestion, as it worked perfectly.