I am using shs on profile2 form. If form validation fails and an error is shown shs js(ajax) wont load the select list and it will be hidden by css. If there is no error after form is submited then shs is working well. Thx

CommentFileSizeAuthor
#5 if_form_error_occurs-2151747-5.patch628 bytes-enzo-
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

stBorchert’s picture

Priority: Major » Normal
Status: Active » Postponed (maintainer needs more info)

I tried with latest version of Drupal (7.24), SHS (7.x-1.x-dev) and Profile2 (7.x-1.3) and its working as expected.
If I (for example) leave field "username" empty and sumbit the form, Drupal reports the error "Username field is required." and the SHS-field is loaded as expected.

Do you have any special configuration or other modules that may cause Javascript errors so SHS won't load the field?

doppel’s picture

This happens to me when having 2 SHS select list, When the form is submitted and returns a validation error the 2nd select list disappears.

Note: I'm using Entityform (7.x-1.4)

-enzo-’s picture

I have the same issue with version 7.x-1.6+34-dev

stBorchert’s picture

Can anyone experiencing this issue please write down his configuration (used modules, widget settings, etc., )? I couldn't reproduce this.

-enzo-’s picture

Status: Postponed (maintainer needs more info) » Needs review
FileSize
628 bytes

Hi @stBorchert

I found and fix the problem.

The problem occurs when you have a field configured as required, but in the form you don't select anything and submit save button of node, the submit process fail and the select is gone.

That is because a logic in widget for SHS in shs_field_widget_afterbuild, the logic don't handle the submit error fail and execute some code disabling parent information in shs js settings.

Attached you can find a patch to fix the module, please validate the value _none, I don't know if is just in my environment.

If you can't wait until this fix is applied by maintainer, imagine you have a module name YOURMODULE, then just copy the following module and all will be fixed.

AGAIN CHECK THE DEFAULT VALUE IF IS _none OR OTHER.

/**
 * Overwrite Afterbuild callback for widgets of type "taxonomy_shs".
 */
function YOURMODULE_shs_field_widget_afterbuild($element, &$form_state) {
  $js_added = &drupal_static(__FUNCTION__ . '_js_added', array());
  // Generate a random hash to avoid merging of settings by drupal_add_js.
  // This is necessary until http://drupal.org/node/208611 lands for D7.
  $js_hash = &drupal_static(__FUNCTION__ . '_js_hash');

  if (empty($js_hash)) {
    $js_hash = _shs_create_hash();
  }

  $parents = array();
  // Get default value from form state and set it to element.
  $default_value = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  if (!empty($default_value)) {
    // Use value from form_state (for example for fields with cardinality = -1).
    $element['#default_value'] = $default_value;
  }

  // Add main Javascript behavior and style only once.
  if (count($js_added) == 0) {
    // Add behavior.
    drupal_add_js(drupal_get_path('module', 'shs') . '/js/shs.js');
    // Add styles.
    drupal_add_css(drupal_get_path('module', 'shs') . '/theme/shs.form.css');
  }

  // Create Javascript settings for the element only if it hasn't been added
  // before.
  if (empty($js_added[$element['#name']][$js_hash])) {
    $element_value = $element['#default_value'];

    // Warranty field render if field is required but not selected.
    if (empty($element_value) || $element_value == '_none') {
      // Add fake parent for new items or field required submit fail.
      $parents[] = array('tid' => 0);
    }
    else {
      $term_parents = taxonomy_get_parents_all($element_value);
      foreach ($term_parents as $term) {
        // Create term lineage.
        $parents[] = array('tid' => $term->tid);
      }
    }

    $vocabularies = $element['#shs_vocabularies'];
    $vocabulary_identifier = NULL;
    if (count($vocabularies) == 1) {
      // Get ID from first (and only) vocabulary.
      $vocabulary_identifier = $vocabularies[0]->vid;
    }
    else {
      $vocabulary_identifier = array(
        'field_name' => $element['#field_name'],
      );
    }

    // Create settings needed for our js magic.
    $settings_js = array(
      'shs' => array(
        "{$element['#name']}" => array(
          $js_hash => array(
            'vid' => $vocabulary_identifier,
            'settings' => $element['#shs_settings'],
            'default_value' => $element['#default_value'],
            'parents' => array_reverse($parents),
            'any_label' => empty($element['#required']) ? t('- None -', array(), array('context' => 'shs')) : t('- Select a value -', array(), array('context' => 'shs')),
            'any_value' => '_none',
          ),
        ),
      ),
    );

    // Add settings.
    drupal_add_js($settings_js, 'setting');

    if (empty($js_added[$element['#name']])) {
      $js_added[$element['#name']] = array();
    }
    $js_added[$element['#name']][$js_hash] = TRUE;
  }

  unset($element['#needs_validation']);
  return $element;
}

function YOURMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
  // Add a css class to widget form elements for all fields of type mytype.
  if ($context['field']['type'] == 'taxonomy_term_reference' && $element['tid']['#field_name'] == 'field_section') {
      $element['tid']['#after_build'] = array('YOURMODULE_shs_field_widget_afterbuild');
  }
}

  • stBorchert committed b5372f2 on 7.x-1.x authored by -enzo-
    Issue #2151747 by -enzo- | mibfire: Fixed If form error occurs shs js...
stBorchert’s picture

Status: Needs review » Fixed

Thanks for your patch, I've committed it to the latest dev.
Even if I couldn't reproduce this behavior the patch makes sense and doesn't break things ;)

Hope this fixes the problem described here ...

Status: Fixed » Closed (fixed)

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