2

我正在尝试验证我的自定义模块表单。即使我使用 hook_validate 或在提交按钮中使用 #validate 属性,验证也会像魅力一样工作。问题是,在验证失败后,表单会丢失我附加到模块的 css 样式。我正在使用 .info 文件将 css 附加到模块上。我还尝试使用 drupal_add_css 或 #attached 属性在验证函数底部重新附加 css。如果有人有同样的问题,请告诉我。我将不胜感激任何想法或任何帮助。谢谢。

4

2 回答 2

3

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.

于 2013-09-25T07:36:50.147 回答
1

使用#afterbuild,这是一个可选的表单元素,称为$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');
}
于 2015-02-19T12:22:43.503 回答