I'm using clientside validation along with the field UI module in a Drupal 7 site to request some custom input fields on user registration. I need to set some custom error messages using the clientside validation modulename_clientside_validation_rule_alter() function. The problem is, the field UI module creates fields with brackets in the name attribute like so: <input name="field_first_name[und][0][value]" />. When I use field_first_name[und][0][value]or $element['value']['#name'](which has the same value) as the first argument of _clientside_validation_set_required(), the custom error message is not being shown—but instead, the default error message. I believe this is because the brackets are not seen as part of the string because when I alter the name attribute to not have any brackets, I can successfully alter the error message. Unfortunately, I can't change the name attribute of the input because that affects many other things in Drupal.

Is there a way to set the error message on an input with brackets or another way to set a custom error message?

Comments

CL-PSL’s picture

Issue summary: View changes
CL-PSL’s picture

Title: How to set a custom error message for an input with brackets in the name » [Solved] How to set a custom error message for an input with brackets in the name

I was able to set these by using the 'form_validate' case instead of the 'element_validate' case.

function [module_name]_clientside_validation_rule_alter(&$js_rules, $element, $context) {  
  switch ($context['type']) {
        case 'form_validate':
              if (in_array('user_register_form_validate', $context['functions'])) {
                  _clientside_validation_set_required(
                  $element['field_last_name']['und']['0']['value']['#name'],
                  $element['field_last_name']['und']['0']['value']['#title'],
                  isset($element['field_last_name']['und']['0']['value']['#required']) ? $element['field_last_name']['und']['0']['value']['#required'] : FALSE,
                  $js_rules,
                  t('Please enter your last name.')
                );
              }
        break;
   }
}
CL-PSL’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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

suraj_vantagode’s picture

It resolves for required type error messages but How to alter mail field error messages?