In GA I'm using fields inside a table and they do not have a #title element. Therefor the form_error ends with The is using the following invalid tokens: [roles-invalid].. You can see the title is missing here. Otherwise I'm not able to set a title for this element or it would get shown in the table listing.

    $form['googleanalytics_custom_var']['slots'][$i]['value'] = array(
      '#default_value' => !empty($googleanalytics_custom_vars['slots'][$i]['value']) ? $googleanalytics_custom_vars['slots'][$i]['value'] : '',
      '#description' => ($token_enabled ? t('The custom variable value. You may use tokens in this field.') : t('The custom variable value.')),
      '#type' => 'textfield',
      '#element_validate' => array('token_element_validate'),
      '#token_types' => array('all'),
    );

How can we solve this and make the form error helpful?

CommentFileSizeAuthor
#11 token_elements.patch706 byteshass
#6 token_d6_add_title_invisible.patch1000 byteshass

Comments

hass’s picture

Title: %element-title in form_error is empty if form element have no title » %element-title in form_error is empty if form element has no title
dave reid’s picture

In reality you shouldn't have form elements without a title as it provides no context to users that have to use screen readers, etc. You should be using:

$form['myitem'] = array(
  '#title' => 'Helpful title',
  '#title_display' => 'invisible',
  ...
);

Core itself seems to assume that #title is present, although not always. _form_validate() when checking the #required property of a field does the following:

form_error($elements, $t('!name cannot be longer than %max characters but is currently %length characters long.', array('!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'], '%max' => $elements['#maxlength'], '%length' => drupal_strlen($elements['#value']))));

But other element validation functions assume $element['#title'] exists:
http://api.drupal.org/api/drupal/modules--field--field.module/function/_...
http://api.drupal.org/api/drupal/modules--field--field.module/function/_...
http://api.drupal.org/api/drupal/modules--field--field.module/function/_...

dave reid’s picture

The D6 version of _form_validate also assumes $element['#title'] always exists.

hass’s picture

I was not aware of #title_display... I go with this in D7, but we cannot for D6.

I found more strange issue in token code... 'token_element_validate_token_context' is deprecated in D6, but in token D7 it is the only available validation function. Can you take a look to this, please? May need a new case...

hass’s picture

Version: 7.x-1.x-dev » 6.x-1.x-dev
Status: Active » Needs review
StatusFileSize
new1000 bytes

D6 patch attached adds $element['#title_invisible'] for D6. Are you able to accept this patch as a workaround for D6, please?

I have tested this and it WFM.

hass’s picture

dave reid’s picture

Committed the proper fix to D7.
http://drupal.org/cvs?commit=477968

dave reid’s picture

Hrm, I don't like '#title_invisible' for D6 since its not really supported. Does the same fix in http://drupal.org/cvs?commit=477968 help with this?

dave reid’s picture

Status: Needs review » Fixed

Confirmed, the same fix is good for Drupal 6, so committed to CVS: http://drupal.org/cvs?commit=477972

hass’s picture

Status: Fixed » Needs review
StatusFileSize
new706 bytes

You have committed a broken patch... it need to be $element, but you used $elements. Patch need to be applied to both D7 and D6.

dave reid’s picture

Status: Needs review » Fixed

Thanks, I had just noticed it too and committed a fix.

Status: Fixed » Closed (fixed)

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