drupal7表单验证添加类?

问题描述 投票:0回答:3

在我的表单中,我创建了一个复选框

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

当我使用hook_validate验证它时,我想给标签添加一个类。有什么办法可以实现这一点吗?

forms drupal-7 validation
3个回答
0
投票

我无法想象为什么你要在验证函数中这样做,我认为有一个更简单的方法来完成你想要做的事情。

Drupal表单中的每个元素都被一个容器(它有一个ID)所包裹。在这个容器中,永远只有一个标签。

所以,如果你需要在CSS或JS中锁定元素,你只需要做这样的事情。

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

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

如果你真的需要手动编辑标签,那么你将不得不为该元素提供一个自定义主题,请看一下 本例 获取更多信息(这是Drupal 6的,但在Drupal 7中的概念是一样的)。


0
投票

谢谢Clive在表单验证功能中做了一个相当讨厌的工作。

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

虽然不好看,但很管用


0
投票

你可以在 hook_validate():

$form_state['complete form']['submitted']['existing_customer']['#attributes']['class'][] = 'class_name';
© www.soinside.com 2019 - 2024. All rights reserved.