How to customize an error message when the user doesn't fill out a mandatory field in webform, I mean, the webform is generated by the webform module and the validation rule is set by webform validation module。

Comments

stupiddingo’s picture

Mandatory is set by the webform module, not webform validation. This text is not editable, and unfortunately the required field and min-length options inside webform validation do not allow a custom error message.

You could add a custom module pretty easily that would do something similar:

webform_validationrequired.module

/**
* Implementation of hook_webform_validation_validators().
*/
function webform_validationrequired_webform_validation_validators() {
  return array(
    'required' => array(
      'name' => "Required with Custom Message",
      'component_types' => array(
        'textfield',
        'textarea',
        'email',
      ),
      'custom_error' => TRUE,
      'description' => t("Required value with a custom error message."),
    ),
  );
}

/**
* Implementation of hook_webform_validation_validate().
*/
function webform_validationrequired_webform_validation_validate($validator_name, $items, $components, $rule) {
  if ($items) {
    switch ($validator_name) {
      case 'required':
          foreach ($items as $key => $val) {
            if (!$val) {
			  $errors[$key] = _webform_validation_i18n_error_message($rule);
            }
        }
		return $errors;
        break;
    }
  }
}

There may be a more elegant way, but this will work. ;)

jjma’s picture

Stringoverides worked for me to customize the message for users. Copy the text from the webform.module that you want to replace and enter it to the stringoveride config page with the text you want to use.

Jon

svendecabooter’s picture

Status: Active » Fixed

Those seem 2 valid solutions to the problem, so marking as fixed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.