-2

I want to disable custom field in form, my field name is field_course_display_ref and it is entity reference.
I try $form['field_course_display_ref']['und']['#attributes']['disabled'] = 'disabled'; but when form submit it return the field_course_display_ref is required , I also try $form['field_course_display_ref']['#disabled'] = TRUE; but not work and field not disabled( user can change it).
also I try to remove #require with $form['field_course_display_ref']['und']['#required']=FALSE; but nothing happen and when form submit, return the field_course_display_ref is required .

How can I disable required field in form ?

I create form in callback of menu ,part of my code

function cdigitdev_pre_register_ym() {
    global $user;
    drupal_add_js(drupal_get_path('module', 'cdigitdev') . '/js/cdigitdev.js', array('scope' => 'footer'));
    $nid = intval(arg(2));
    $productId = intval(arg(3)); //it will set to null if arg(3) not set ; equal to $productId=arg(3)? intval(arg(2)):NULL;
    module_load_include('inc', 'node', 'node.pages');
    $form = node_add('pre_register');
    $form['field_course_schedules']['#disabled'] = TRUE;
    //$form['field_course_schedules']['und']['#disabled'] = TRUE;
.
.
.
.
    return drupal_render($form);
} 
  • Do hook form alter and change #required to false... Ideally $form['field_course_display_ref']['#required']=FALSE; – Anil Sagar Mar 5 '14 at 6:27
  • it is set to false field_course_display_ref (Array, 17 elements) #attributes (Array, 1 element) #weight (String, 1 characters ) 1 #tree (Boolean) TRUE #language (String, 3 characters ) und und (Array, 38 elements) #access (Boolean) TRUE #theme_wrappers (Array, 1 element) #process (Array, 1 element) #defaults_loaded (Boolean) TRUE #parents (Array, 1 element) #array_parents (Array, 1 element) #processed (Boolean) TRUE #required (Boolean) FALSE #title_display (String, 6 characters ) before #id (String, 29 characters ) edit-field-course-display-ref #sorted (Boolean) TRUE – Yusef Mohamadi Mar 5 '14 at 6:30
  • @AnilSagar how can I disable field? – Yusef Mohamadi Mar 5 '14 at 6:34
  • 1
    Update your question with your form implementation code.. Is your form a content type or custom form ? – Anil Sagar Mar 5 '14 at 6:36
  • @AnilSagar it updated – Yusef Mohamadi Mar 5 '14 at 6:44
-1

I found my problem, in this manner create form with :
module_load_include('inc', 'node', 'node.pages'); $form = node_add('pre_register'); #required=false and #disabled=true not work.

I remove them form my function and put them in form_alter and work correctly , both #required and #disabled .

My problem resolved but really I dont understand why it not work in my function and in hook_form_alter work .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.