tags:

views:

207

answers:

2

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 unable 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

+1  A: 

Hi there,

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.

sirhc
ya its email and no need for return. Thank u chris ....
Paniyar
A: 

What is emailvalidate(0), phonevalidate(0), titlevalidate(0)? And the form submit vakues correctly? How to populate select tyoe and send value at submit form?

Danzisi

related questions