drupal 7 form validate add class?

狂风中的少年 提交于 2020-01-06 13:26:24

问题


In my form I create a checkbox

$form['existing_customer'] = array(
'#type' => 'checkbox',
'#title' => t('Are you an existing customer?')
);

When I validate it using hook_validate I would like to add a class to the label? Any ideas how to achieve this?


回答1:


I can't imagine why you'd want to do this in a validation function, and I think there's a far easier way to accomplish what you're trying to do.

Each element in a Drupal form is wrapped with a container (which has an ID). Inside this container there will only ever be one label.

So if you need to target the element in CSS or JS you just need to do something like this:

#existing-customer-edit label {
  // The rule
}

OR

$('#existing-customer-edit label').something();

If you really need to edit the label manually then you're going to have to provide a custom theme for that element, have a look at this example for more information (it's for Drupal 6 but the concept is the same in Drupal 7).




回答2:


thanks Clive did a fairly nasty work around in the form validation function

$form_state['complete form']['myselectbox']['#title'] =  '<span class="privacy-error">you did not check me</span>';

It ain't pretty but it works!



来源:https://stackoverflow.com/questions/8340175/drupal-7-form-validate-add-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!