Set conditonal default value of a field I have a custom form with a field...

function mh_integration_connect_form($form, &amp;$form_state) {
  $form['#method'] = 'get';
  $params = drupal_get_query_parameters();
  $form['cat'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; t('Categories'),
    '#options' =&gt; $categories,
    '#default_value' =&gt; $params['cat'],
  );
}


as $param is not set before the form is submitted, so it show error for "Notice: Undefined index: cat".
I am wondering if we can set this default_value conditionally, 

if(isset($params['cat'])) {
    '#default_value' =&gt; $params['cat'],
}
Here is how you can do it:

  $params = drupal_get_query_parameters();
  $form['cat'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; t('Categories'),
    '#options' =&gt; $categories,
  );

  if(isset($params['cat'])) {
    $form['cat']['#default_value'] = $params['cat'];
  }