0

Is there any way to disable the field 'model_article_nr' until 'model' is selected

this is what i have and as it is now i can select 'model_article_nr' before i have choosen a model which leads to wrong article nummer if i change model afterwords, because when no model is selected the first model is standard in my '$article_list[$curent_model]' and beeing displayed as options in 'model_article_number'

$model_list = array();
$article_list = get_model_article_list();

foreach($article_list as $key => $value) {
        if ( empty($curent_model) ) $curent_model = $key;
        $model_list[$key] = $key;
    }

$form['model'] = array(
        '#type' => 'select',
        '#title' => t('Model'),
        '#title_display' => 'invisible',
        '#empty_option' => t('Select Model'),
        '#options' => $model_list,
        '#ajax' => array(
            'callback' => 'form_model_callback',
            'wrapper' => 'article-number-ajax-wrapper',
        ),
        '#prefix' => '<div class="clearfix"></div><div class="form-helper-container">',
        '#suffix' => '<button type="button" class="btn tooltip-btn" data-toggle="modal" data-target="#model-help"><span class="glyphicon glyphicon-question-sign"></span></button></div>',
    );

    $form['model_article_nr'] = array(
        '#type' => 'select',
        '#title' => t('Article Number'),
        '#title_display' => 'invisible',
        '#attributes' => array('placeholder' => t('Article Number'), 'class' => array('space-bottom')),
        '#empty_option' => t('Select Article Number'),
        '#validated' => TRUE,
        '#options' => $article_list[$curent_model],
        '#prefix' => "<div id='article-number-ajax-wrapper'>",
        '#suffix' => "</div>",
    );

And my callback

function form_model_callback($form, &$form_state) {
    return $form['model_article_nr'];
}
0

As @leymannx stated you can achieve this using #states. Here is a simple example:



 $form['show_textbox'] = array(
  '#type' => 'checkbox',
  '#title' => t('Show text Box 1'),
        '#attributes' => [
        'id' => 'show_textbox',
        'name' => 'field_show_textbox',
      ],
);

    $form['text_box1'] = [
      '#type' => 'textfield',
      '#size' => '60',
      '#placeholder' => 'Enter Name',
      '#attributes' => [
        'id' => 'text_box1',
        'autocorrect' => 'none',
        'class' => 'text_box1',
        'autocapitalize' => 'none',
        'spellcheck' => 'false',
      ],
      '#states' => [
        //show this textfield only if the radio button 'show_textbox1' is selected
        'visible' => [
                ':input[name="field_show_textbox"]' => array('checked' => TRUE),
        ],
      ],
    ];

You can always change the condition.

0

Thank you Hunde Tekle and leymannx it works :D

$form['model'] = array(
        '#type' => 'select',
        '#title' => t('Model'),
        '#title_display' => 'invisible',
        //'#attributes' => array('placeholder' => t('Model')),
        '#empty_option' => t('Select Model'),
        '#options' => $model_list,
        //'#default_value' => $curent_model,
        //'#disabled' => TRUE,
        '#ajax' => array(
            'callback' => 'form_model_callback',
            'wrapper' => 'article-number-ajax-wrapper',
        ),
        '#prefix' => '<div class="clearfix"></div><div class="form-helper-container">',
        '#suffix' => '<button type="button" class="btn tooltip-btn" data-toggle="modal" data-target="#model-help"><span class="glyphicon glyphicon-question-sign"></span></button></div>',
    );

    $form['model_article_nr'] = array(
        '#type' => 'select',
        '#title' => t('Article Number'),
        '#title_display' => 'invisible',
        '#attributes' => array('placeholder' => t('Article Number'), 'class' => array('space-bottom')),
        '#empty_option' => t('Select Article Number'),
            '#states' => array(
            'visible' => array(
              array(
                'select[name="model"]' => array('!value' => ''),
              ),
            ),
        ),
        '#validated' => TRUE,
        '#options' => $article_list[$curent_model],
        '#prefix' => "<div id='article-number-ajax-wrapper'>",
        '#suffix' => "</div>",
    );

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.