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));
}
}
}
}
}
}
}
}
Comment | File | Size | Author |
---|---|---|---|
#6 | 2184735-cck_phone-fix-is-required-validation-empty-error-msg.patch | 652 bytes | PaulDinelle |
Comments
Comment #1
lord_of_freaks CreditAttribution: lord_of_freaks commentedHi
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.
Comment #2
a.hjelm CreditAttribution: a.hjelm commentedHi,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.
Comment #3
a.hjelm CreditAttribution: a.hjelm commentedWell 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.
Comment #4
josebc CreditAttribution: josebc commentedcould not apply patch, could you please check patch
Comment #5
RayCascellaThe easiest patch fix is to update the commented out '#title' code to the following:
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.
Comment #6
PaulDinelle CreditAttribution: PaulDinelle at Fenix Solutions Inc commentedMade a patch with RayCascella's suggestion, as it worked perfectly.