hi i need to know how to add attribute onchange to a custom content_type field?
For ex my content_type has 2 fields phone (name:field_phone[0][value], id:edit-field-phone-0-value),email (name:field_email[0][value], id:edit-field-email-0-value). i'm una开发者_运维百科ble to add attribute as follows.
function knpevents_form_event_node_form_alter(&$form, &$form_state) {
$form['title']['#attributes'] = array('onchange' => "return titlevalidate(0)");//fine
$form['field_phone[0][value]']['#attributes']= array('onchange' => "return phonevalidate(0)"); //error
$form['field_emai[0][value]']['#attributes']= array('onchange' => "return emailvalidate(0)"); //error
}
how to add it
Altering the forms with CCK widgets requires a bit more tweaking as mentioned on this Book page. Since during hook_form_alter
, the CCK fields are not yet processed.
Your code should probably look like (I'm not sure if the emai
was intentional spelling):
function knpevents_form_event_node_form_alter(&$form, &$form_state) {
$form['title']['#attributes'] = array(
'onchange' => "return titlevalidate(0)"
);
$form['#after_build'][] = 'knpevents_form_event_node_form_cck_alter';
}
function knpevents_form_event_node_form_cck_alter($form, &$form_state) {
$form['field_phone'][0]['value']['#attributes'] = array(
'onchange' => "return phonevalidate(0)"
);
$form['field_emai'][0]['value']['#attributes'] = array(
'onchange' => "return emailvalidate(0)"
);
return $form;
}
Also, I don't think you need to put the return
there. titlevalidate(0);
should do just fine.
精彩评论