Thursday, May 26, 2011

make CCK field required

make CCK field required

Since CCK field shared the same global "required" setting, we can hook_form_alter() to make the field required in some situations.

<?php
/**
 * Implementation of hook_form_alter().
 */

function test_form_alter(&$form, $form_state, $form_id) {
  ### make nid_contact field required.
  ### Note: you need following both lines in order to make it work properly.
  $form['field_nid_contact']['#required'] = TRUE;
  $form['field_nid_contact'][0]['#required'] = TRUE;
}
?>

No comments: