As the title says, when submitting a form, if you dont fills all required fields (controlled or not) and validation fails, drupal will shows you again the form, but this time the conditional fields are not working (all fields are visible and dont change when the value of controller fields change).

Comments

flapsjack’s picture

Maxtorete,

This is caused by the conditional_fields.js not being added to the page. The problem originally was that the js was being added at hook_form_alter, but hook_form_alter is not called when the form is being rebuilt in the case of validation. So, in the latest versions of the code, peterpoe has additionally added a call to the hook_validate function. However, the problem that I have seen is with the following if statement:

  // When previewing we need to add js here, otherwise we risk to add it twice
  if ($form['#conditional_fields']['settings']['ui_settings'] && $form_state['values']['op'] == $form_state['values']['preview']) {
    conditional_fields_add_js($form['#conditional_fields']['settings']);
  }

The first condition passes but the second one only passes if you click on the Preview submit button. In the case that you press enter in one of the textfields, the value of $form_state['values']['op'] is Save which is the same value you get when you click on the Save button. However, the value of $form_state['values']['preview'] seems to always be Preview. So, the if statement only is true and subsequently the js is only added when you click on Preview.

The question now is should the if statement be adjusted, or is there some expected behavior that isn't happening. I am not exactly sure why the second condition was added to this if statement. It wasn't there in previous versions, just the check to see if the ui_settings were present. Maybe peterpoe can shed some light on this issue.

One solution that I see, and have posted a patch for at http://drupal.org/node/350411 is to modify the conditional_fields_add_js function to check and see if it has already added the js to the page. This would allow for the validate hook to just add the js and then the conditional_fields_add_js function determines if the js in question has already been added and acts accordingly. This behavior is similar to drupal_render in the way that it remembers what form fields have been rendered so that they are not rendered multiple times.

j.slemmer’s picture

I'm having the same issue, all conditional fields are displaying instead of hidden.
Are the patches at http://drupal.org/node/350411 already integrated in the current dev version?

Or might they offer some solutions to this problem?

peterpoe’s picture

Status: Active » Closed (duplicate)

@flapsjack: Now I understand. I simply had never tried to press enter or click directly on the save button :)

Looks like the best solution is, following your idea, adding a static variable to conditional_fields_add_js

But let's continue the discussion in the original bug: #350411: Validation and Javascript not being added
I'll write a patch and post it there shortly.