1

I am new to drupal i want to set auto complete off in my following code

$form['plantuser']['username'] = array(
    '#type' => 'textfield',
    '#title' => t('Username'),
    '#description' => t(''),
    '#required' => true,
    '#maxlength' => 50,
     '#weight' =>1,
  );
1
  • You are going to have to give more information. What are you trying to do? Do you just want to do this for a single field (if so where is the field, a node form, a views exposed form, somewhere else?), or for all fields on the site?
    – rooby
    Commented Apr 21, 2013 at 7:20

3 Answers 3

6

That code doesn't enable Drupal's autocomplete (it would need the #autocomplete_path property to do so), so I guess you mean the browser autocomplete?

The standard way to do that is to set the autocomplete attribute on the element which you can do like this:

  $form['plantuser']['username'] = array(
    '#type' => 'textfield',
    '#title' => t('Username'),
    '#description' => t(''),
    '#required' => true,
    '#maxlength' => 50,
    '#weight' =>1,
    '#attributes' => array('autocomplete' => 'off'),
  );

Bear in mind autocomplete is only valid using the HTML5 doctype; if you're using another doctype you'll need to implement a JS solution to set the attribute instead, e.g.

(function($) {
  $(function() {
    $('#input-id').attr('autocomplete', 'off');
  });
})(jQuery);
0

the form does not have autocomplete feature by default.May be when you type in the field the the suggestions are populated from browser cache so you can test it again on other browser or on same brwoser after clearing the cache.

2
  • 2
    All major browsers implement a client-side autocomplete feature
    – Clive
    Commented Apr 17, 2013 at 9:46
  • I was in doubt if he was assuming the browser auto complete feature as the default drupal autocomplete.
    – arpitr
    Commented Apr 17, 2013 at 12:56
0

Another way to do this if you are only targeting one element and don't need an array:

your_module_form_alter(&$form, &$form_state, $form_id) {

    if ($form['#id'] == 'the_id_of_your_form'){
        $form['the_id_of_your_form']['#attributes']['autocomplete'] = 'off';
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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