1

I have a form built with the Form API. I have a field which is, at first, disabled. I want to change the status of that field to enabled when the user selects another contiguous field.

I have a working example of changing the callback URL of Ajax complete of that field, so the form is making a kind of OnChange and working well, but for some reason I am not able to change the disabled status.

Code. Here the 2 elements, first brand and next the model, which is disabled at the beginning.

 $form['brand'] = array(
'#type' => 'select',
'#title' => t('Brand'),
'#options' => $brands,
'#default_value' => $brand,
'#ajax' => array(
    'callback' => 'hook_models_update',
    'wrapper' => 'models_wrapper',
    'progress' => array(
        'message' => '',
        'type' => 'throbber',
    ),
),
);

 $form['model'] = array(
    '#type' => 'textfield',
    '#title' => t('Model'),
  //    '#description' => 'Please note available models change based on the brand selected.',
'#required' => FALSE,
'#autocomplete_path' => 'providers/autocomplete/' . $brand,
'#attributes' => array("placeholder" => t('Model')),
'#prefix' => '<div id="models_wrapper">',
'#suffix' => '</div>',
'#disabled' => TRUE,
);

Now the code for the "onChange" behaviour, which is a "throbber" called hook_models_update:

function hook_models_update($form, &$form_state) {
    unset($form_state['input']['model'], $form_state['values']['model']);
    $brand = $form_state['values']['brand'];
    $form['model']['#autocomplete_path'] = 'providers/autocomplete/' . $brand;
     $form['model']['#enabled'] = TRUE;
    // $form['model']["#disabled'] = FALSE
    return form_builder($form['#id'], $form['model'], $form_state);
} // function

I have tried with #enabled = TRUE and with #disabled = FALSE, with no successs.

6
  • I haven't seen form_builder used in an AJAX callback before, is that something you've seen in core/contrib? Not saying it's wrong, just never seen it. Usually you just return the element (return $form['model'];) It's definitely '#disabled' => FALSE BTW, there isn't an #enabled property – Clive Dec 3 '15 at 18:15
  • I don't recall where I saw it, it took me quite a lot to find it, and it is working perfectly in spite of all. I have solved the problem, I didn't find the right way of making the question, finally I found the solution, of course in this site. – Cesar Dec 3 '15 at 18:28
  • can you post the answer? – Adrian Cid Almaguer Dec 3 '15 at 18:31
  • Yeah I'm sure it'll work (if you return the element directly I'd wager the form system runs it through form_builder anyway). What was the problem out of interest? – Clive Dec 3 '15 at 18:31
  • It was done to refresh the autocomplete path of the form, so when the user select a brand, the autocomplete model URL changes dinamically (Javascript) so it only suggests those models of that selected brand. I didn't find more than this solution after quite a lot of time searching ! – Cesar Dec 3 '15 at 18:33
2

I found the solution here:

Enable DropDown using Ajax Call

The code finally gets this:

     function hook_models_update($form, &$form_state) {
         unset($form_state['input']['model'], $form_state['values']['model']);
         $brand = $form_state['values']['brand'];
         $form['model']['#autocomplete_path'] = 'providers/autocomplete/' . $brand;
         unset($form['model']["#attributes"]['disabled']);
         $form['model']['#disabled'] = FALSE;
         return form_builder($form['#id'], $form['model'], $form_state);
} // function

The trick lies in these two lines:

     unset($form['model']["#attributes"]['disabled']);
     $form['model']['#disabled'] = FALSE;

Apparently, there has to be an unset and not of #disabled, but #attributes - disabled. Why this is so convoluted is beyong my comprehension.

2
  • Good find! I'm pretty sure you've found a small bug in form API - #attributes should really be re-processed if there's a chance that property changes could have affected them – Clive Dec 3 '15 at 18:36
  • Well, I don't think I have found anything, cause the problem and solution was already in that post, I simply had a need. But I agree that if may be a small bug in the form API. – Cesar Dec 4 '15 at 9:39

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.