1

I am adding some custom fields to a registration page using the field UI module. I'm also using the clientside validation module to display error messages on required fields. I am able to change the error messages for the default fields I have not added using the field UI, but not for the fields added with the field UI. I have tried many different ways of targeting the fields to no avail. I'm using Drupal 7.

I can see the custom rule is actually being set when I kpr($js_rules):

$js_rules['field_first_name']['messages']['required'] <span data-thmr='thmr_13' class='devel-themer-wrapper'>Please enter your first name.</span

...but the default error message still shows. I have a feeling this is due to the <input> element's name attribute being field_first_name[und][0][value], but I was even unsuccessful in targeting this rule using that string in the _clientside_validation_set_required() function.

The difference between the default fields and my custom fileds are the custom fields values are very heavily nested within multiple arrays.

However, I'm unsuccessful in changing the input element's name attribute using any of the following:

$form['field_first_name'][#name], $form['field_first_name'][#field_name], $form['field_first_name']['und'][0]['value'][#field_name], $form['field_first_name'][#name], etc.

My custom rules:

_clientside_validation_set_required(
  $element['#name'],
  $element['#title'],
  isset($element['#required']) ? $element['#required'] : FALSE,
  $js_rules,
  t('Please enter your company email.')
);

Which works for the drupal default email field. However, this (or any of the many ways I've tried referencing the element's name) does not change the error message for my custom "first name" field:

_clientside_validation_set_required(
  $element['#field_name'],
  $element['#title'],
  isset($element['#required']) ? $element['#required'] : FALSE,
  $js_rules,
  t('Please enter your first name.')
);

Update

It turns out it IS the name attribute. When I hard code the name as field_first_name using theme_textfield(), my custom error message shows up. The problem with this is it prevents the proper values from being written to the database. Is there a way to escape the brackets in field_first_name[und][0][value] in order to use that as the first argument of _clientside_validation_set_required()?

0

I was able to set the rule by referencing the nested #name and #value keys:

_clientside_validation_set_required(
      $element['field_first_name']['und']['0']['value']['#name'],
      $element['field_first_name']['und']['0']['value']['#title'],
      isset($element['field_first_name']['und']['0']['value']['#required']) ? $element['field_first_name']['und']['0']['value']['#required'] : FALSE,
      $js_rules,
      t('Please enter your @field_name.', array('@field_name' => strtolower($element['field_first_name']['und']['0']['value']['#title'])))
    );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.