Question

I am trying to validate my custom module form. The validation works like charm even if I use hook_validate or use the #validate attribute in the submit button. The problem is that after validation failed the form losses the css styles I have attached to the module. I am attaching the css on module using the .info file. I have also tried to re attache the css at the bottom of the validation function using drupal_add_css or the #attached attribute. If anyone had the same issue please let me know. I will appreciate any ideas or any help. Thanks.

Was it helpful?

Solution

If someone having the same problem I found the solution. After validation Drupal changes the html form id. If you are using css selectors based on the form id, just add a custom class name to your form using the #attributes form attribute somewhere in your form creating function.

$form['#attributes'] = array('class' => array('your_id'));

Then use this class for various selections in your css file.

OR

You can always use attribute CSS selectors with wildcards.

OTHER TIPS

Use #afterbuild, this is an optional form element called $form['#after_build'].

function yourmodulename_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'thisformid'){
    $form['#after_build'][] = 'yourmodulename_after_build';
  }
}

function yourmodulename_after_build($form, &$form_state) {
  drupal_add_css('your_file.css');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top