Hi I was wondering if anyone could give me some advice on this. Basically what I have is a custom content type created by the content creator GUI, I am writing a custom module to alter the appearance and values of the content form.

like so:

function sswap_content_validation_form_alter(&$form, $form_state, $form_id) 
if (isset($form['type']) && isset($form['#node'])) 

if ($form_id == 'advertise_node_form') 

  // Unset the options we don't want user to see.

  $form['#after_build'][] = '_surplus_cck_form_alter';

  // we need to tell the form to follow a validation function
  $form['#validate'][] = '_surplus_cck_validation';
  $form['#submit'][] = '_surplus_cck_submit';

This all works fine and I am able to alter values in the form_alter section as expected, however once I get to the _surplus_cck_validation custom function I am unable to change values, basically what I am doing is checking if a value is X if not I'll replace it with something more suitable after some processing. At this point I can't even change X to a static string though.

To alter my form content I am using:

$form['field_adpostcode']['und'][0]['value']['#value'] = 'TESTING';
$form['field_adpostcode']['und'][0]['#value']['value'] = 'TESTING';

These lines do actually work in the form_alter part as mentioned. I should also note the function for surplus_cck_validation is being called I have experimented with a few echos and notices, and the $form and &$form_state are being passed to it also.

Any help on this would be greatly appreciated I've spent the last few hours trying different things and digging down into the form structure with dsm and debug, but can't seem to find what I need.

  • Hello, and welcome on Drupal Answers. You wrote that those lines work. What is your question, then?
    – apaderno
    Commented Oct 25, 2012 at 16:31
  • Thanks for the welcome, the issue is that they work in form_alter but not in the custom validate function. I'm assuming something changes in the form structure when the validate is called, Sisko's answer helps in that it alters how the value is stored when it is viewed as a node, but then when you go to edit it the old values remain. Commented Oct 25, 2012 at 18:55

1 Answer 1


Perhaps try $form_state.

$form_state['field_adpostcode']['und'][0]['value']['#value'] = 'TESTING';
$form_state['field_adpostcode']['und'][0]['#value']['value'] = 'TESTING';

I suspect the array structure is different but you get the idea.

  • I was lead to believe that form_state was simply for checking values and not altering them? This is just what I read from the Pro Drupal Development book I've been reading, I will give it a try though, thanks! Commented Oct 25, 2012 at 15:17
  • Well it worked to an extent, while viewing the node the value assigned via $form_state['values']['field_adpostcode']['und'][0]['value'] = 'TESTING'; is visible, however if you go to edit the form these values are what they originally where. Commented Oct 25, 2012 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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