I am performing a form_alter on a node/add form for a CCK type name service _request.

 <?php
function addSR_form_service_request_node_form_alter(&$form, $form_state) {
       if (arg(0) == 'user' && is_numeric(arg(1))) {
        $account = arg(1);
        $club = 2589;
        $form['field_sr_account'] = array( '#type' => 'hidden',
        '#value' => $club
        );

           }
}

The form alter is working, because in the HTML source, the input is "Hidden" and it's value is "2589" - However, when the field is submitted to the database, the column in the content_type_service_request is "2" instead of "2589" - I have tried this with several different numbers, and it always yields the first number of the variable.

-The column type in DB is integer(4)
-I have tried #default_value as well

Comments

markus_petrux’s picture

Category: bug » support
Status: Active » Fixed

The bug is in your code, not in CCK. So this is support request here. ;-)

You should take into account the delta of the fields. Fields in the form are often organized into arrays (keyed by delta), where each element is a single item. However, the structure may vary from field to field. In case of doubt, you should do a var_dump() from your implementation of hook_form_alter() to figure it out. Once you know, the structure, then you can change it.

Also, I would not recommend to override the whole element as:

$form['field_sr_account'] = array( ... );

...but to change only the attributes that you really need to change. Otherwise, you'll break the field in the form.

bigplanet’s picture

Ok, I tried adding in [43] before the [0], with [#value] on the end.

I'm trying something like this, but still no go...:

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

I'm still trying to figure out what went wrong? Is it that I gave it an array, but is was expecting an array of array's?
here is what I get on this field when I do a Vardump.

 [43] => Array
        (
            [#type] => hidden
            [#default_value] => 2589
            [#post] => Array
                (
                )

            [#programmed] => 
            [#tree] => 
            [#parents] => Array
                (
                    [0] => field_sr_account
                )

            [#array_parents] => Array
                (
                    [0] => field_sr_account
                )

            [#weight] => 0.016
            [#processed] => 1
            [#description] => 
            [#attributes] => Array
                (
                )

            [#required] => 
            [#input] => 1
            [#process] => Array
                (
                    [0] => form_expand_ahah
                )

            [#name] => field_sr_account
            [#id] => edit-field-sr-account
            [#value] => 2589
            [#defaults_loaded] => 1
            [#sorted] => 1
        )
danielb’s picture

I'm having a similar problem, I have written a cck widget, and it only saves the first letter
this thread didn't help, I already understand there are deltas, but it's still confusing since it seems this decision is being made by cck not by me

danielb’s picture

Uh ok I fixed it

my form element module normally sets the value like this in the validation step:

$element['#value'] = $the_bloody_value;

now it has to be

$element['#value'] = array('value' => $the_bloody_value);

bit strange, but ok...

except that will ruin my widget for non-cck purposes, i think i need a seperate validation callback for cck, since there is nothing in the $element that explicity says "I'm a CCK field".

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.