drupal 7 form validate add class?

2019-08-26 10:32发布

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?

2条回答
Bombasti
2楼-- · 2019-08-26 10:44

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).

查看更多
何必那么认真
3楼-- · 2019-08-26 11:03

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!

查看更多
登录 后发表回答