Thursday, April 21, 2011

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?

From stackoverflow
  • 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 .

    cinqoTimo : I've used both. I believe #value is the correct parameter...
  • 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']));
    

0 comments:

Post a Comment