Answers.Body: <p>Try <code>$element-&gt;updateAttributes(array('readonly'=&gt;true));</code></p>
  
  <p>The difference between readonly and disabled is a common thing with HTML and forms.</p>
  
  <p>See for example <a href="https://stackoverflow.com/questions/7730695/whats-the-difference-between-disabled-disabled-and-readonly-readonly-for-ht">https://stackoverflow.com/questions/7730695/whats-the-difference-between-disabled-disabled-and-readonly-readonly-for-ht</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-07T01:54:45.617Z
Answers.LastActivityDate: 2019-11-07T01:54:45.617Z
Answers.UserDisplayName: Demerit
Body: <p>I would like to set a read only field in Drupal CiviCRM:</p>
  
  <pre><code>mymodule_civicrm_buildForm($formName, &amp;$form) {
    ...
  
    // Set default value
    $defaults['readonly_field'] = xxx;
    $form-&gt;setDefaults($defaults);
  
    // Set read only
    $element = $form-&gt;getElement('event_type_id');
    $element-&gt;updateAttributes(array('disabled'=&gt;true));
  }
  </code></pre>
  
  <p>The field is disabled for input; however, the default value cannot be passed through the form submission.</p>
  
  <p>The form warns that this required field does not input.</p>
  
  <p>Could you advise how to set the read only field correctly?</p>
  
  <p>Thanks a lot!!</p>
  
Tags: <drupal><php><civicrm-home>
Title: How to set read only field in Drupal CiviCRM
ThreadText: # How to set read only field in Drupal CiviCRM
  
  (Asked by: username_0 on 2019-11-07)
  
  I would like to set a read only field in Drupal CiviCRM:
  mymodule_civicrm_buildForm($formName, &$form) {
    ...
  
    // Set default value
    $defaults['readonly_field'] = xxx;
    $form->setDefaults($defaults);
  
    // Set read only
    $element = $form->getElement('event_type_id');
    $element->updateAttributes(array('disabled'=>true));
  }
  
  The field is disabled for input; however, the default value cannot be passed through the form submission.
  The form warns that this required field does not input.
  Could you advise how to set the read only field correctly?
  Thanks a lot!!
  
  **Tags:** drupal, php, civicrm-home
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-11-07. Score: 2
  
  Try `$element->updateAttributes(array('readonly'=>true));`
  
  The difference between readonly and disabled is a common thing with HTML and forms.
  
  See for example <https://stackoverflow.com/questions/7730695/whats-the-difference-between-disabled-disabled-and-readonly-readonly-for-ht>
  
  
  ---
  
  