I am having a simple form like this. I want to read the ID value in the middle of the form. When I try to get it using $fromvalues = $form_state['values']['id']; it gives the following error:

Undefined Index: values in templates_form()

function templates_form_add($form, &$form_state, $templates = NULL) {
  $templates = entity_get_controller('templates')->create();     
  return templates_form($form, $form_state, $templates); 

 function templates_form($form, &$form_state, $templates) {
   $form = array();

   $form['id'] = array(
     '#title' => t('ID'),
     '#type' => 'textfield',
     '#default_value' => isset($templates->id) ? $templates->id : '',
     '#description' => t('Unique Id'),
     '#required' => TRUE,
     '#maxlength' => 255,
     '#disabled'=> empty($ntemplates->id) ? FALSE : TRUE,

   $fromvalues= $form_state['values'];

   $form['city'] = array(
     '#title' => t('city'),
     '#type' => 'select',
     '#options' => $cities,
     '#default_value' => isset($templates->city) ? $templates->city: '',
     '#required' => TRUE,
     '#maxlength' => 255,
     '#disabled'=> empty($templates->id) ? FALSE : TRUE,


The error indicates precisely what is going on: during the initial call of templates_form() the $form_state['values'] does not yet exist. This will exist in a future ajax call (if you do ajax) and in validate and submit functions. You better check if it exists first with isset or empty.

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