I have a text field which displays the username. The user should not be able to edit the text field values. Can anyone help me on this?

Thanks,
Sagar.

Comments

amnon’s picture

Where exactly does this field appear?

If it's the one in the profile page, you should go to the permissions settings and un-check the relevant permission.

phuc77’s picture

Hi,

You could try adding one of the following attributes to your text field:

  $form['username'] = array(
    '#type' => 'textfield',
    '#title' => t('username'),
    '#attributes' => array('readonly' => 'readonly'),
  );

http://www.w3.org/TR/html4/interact/forms.html#adef-readonly

  $form['username'] = array(
    '#type' => 'textfield',
    '#title' => t('username'),
    '#attributes' => array('disabled' => 'disabled'),
  );

http://www.w3.org/TR/html4/interact/forms.html#adef-disabled

- p

meetsagar’s picture

I am able to disable the text field.
Thanks for the quick reply. Thanks a lot.

Thanks,
Sagar.

hanching’s picture

Thank your.

I have been trying

'#readonly' => ture,
'#readonly' => readonly,

Is not correct..

but use '#disabled' => ture, Is feasible

Thank you very much ..
'#attributes' => array('readonly' => 'readonly'),

dongtian’s picture

'#disabled' => TRUE, is work

wismbuh’s picture

edian tenan...
it's work, the field became grey, thank's

surendra77c’s picture

function alter_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'article_node_form': // content type article check with dsm($form)
global $user;
if (in_array('editor', array_values($user->roles))) {
$form['title']['#disabled'] = TRUE;
}
break;
}
}

vikrama1k1’s picture

$form['billing-address'] = array(
'#type'=> 'textfield',
'#title' => t('Your Billing Address is '),
//'#disabled' => TRUE, its working & its show grey color
'#attributes' => array('readonly' => 'readonly'),
'#default_value' =>$bill,
);