tpow
tpow

Reputation: 7894

Drupal 6: Working with Hidden Fields

I am working on an issue i'm having with hooking a field, setting the default value, and making it hidden. The problem is that it is taking the default value, but only submitting the first character of the value to the database.

//Here is how I'm doing it
$form['field_sr_account'] = array( '#type' => 'hidden', '#value' => '45');

I suppose there is something wrong with the way that I have structured my array, but I can't seem to get it. I found a post, http://drupal.org/node/59660 , where someone found a solution to only the first character being submitted

//Here is the format of the solution to the post - but it's not hidden
$form['field_sr_account'][0]['#default_value']['value'] = '45';

How can I add the hidden attribute to this?

Upvotes: 0

Views: 7293

Answers (3)

nobarte
nobarte

Reputation: 11

An interesting solution from http://drupal.org/node/257431#comment-2057358

CCK Hidden Fields

/**
* Implementation of hook_form_alter().
*/
function YourModuleName_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']) && isset($form['#node'])) {
    ### Make a CCK field becoming a hidden type field.
    // ### Use this check to match node edit form for a particular content type.
    if ($form_id === 'YourContentTypeName_node_form') {
      $form['#after_build'] = array('_test_set_cck_field_to_hidden');
    }
  }
}

function _test_set_cck_field_to_hidden($form, &$form_state) {
  $form['field_NameToBeHidden'][0]['value']['#type'] = 'hidden';
  $form['field_NameToBeHidden'][0]['#value']['value'] = 'testValue';

  return $form;
}

Upvotes: 1

tpow
tpow

Reputation: 7894

The answer was actually to set the value and the hidden attribute separately, then set the value again in the submit handler using the following format.

I'm not sure if it's all necessary, I suppose I probably don't need to assign it in the form alter, but it works, so I'm going to leave it alone...

$form['#field_sr_account'] = $club;
    $form['field_sr_account'] = array( '#type' => 'hidden','#value' => $club);
   }
}

/*in submit handler, restore the value in the proper format*/
$form_state['values']['field_sr_account'] = array('0' => array('value' => $form['#field_sr_account']));

Upvotes: 1

Januz
Januz

Reputation: 527

Have you tried using #default_value insted of #value?

Also if you're trying to pass some data to the submit that will not be changed in the form you should use http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html#value .

Upvotes: 2

Related Questions