0

I currently have a custom module which calls a submit custom validation function. I am trying to update a value after the validation is run. Basically, if a value is present, update a field to say "Yes".

function sitefuncs_form_alter(&$form, &$form_state, $form_id){

if( $form_id == 'stage_2_node_form') {
   $form['actions']['submit']['#submit'][] = 'sitefuncs_custom_submit';
}

} // end hook_form_alter()

function sitefuncs_custom_submit($form, &$form_state) {

        $banned = array(
        'better cures',
        'better nhs',
        'because it',
        'so what'
        );

  if (in_array($form_state['values']['field_stage_two_question_one']['und'][0]['value'], $banned)) { // if a banned word is the value of the input

$form_state['values']['field_stage_complete_text']['und'][0]['value'] = "No"; // set the value of the text input to No

} else {

$form_state['values']['field_stage_complete_text']['und'][0]['value'] = "Yes";

$messages = drupal_get_messages('status');
drupal_set_message(t('Your changes have been saved.'));

}

}

If anyone has any idea why the $form_state isn't updating the fields after I save the node, any help would be great!

2 Answers 2

0

I worked out that I was using

   $form['actions']['submit']['#submit'][] = 'sitefuncs_custom_submit';

When it should have been

   $form['actions']['submit']['#validate'][] = 'sitefuncs_custom_submit';

Then all my data saved using

$form_state['values']['field_stage_complete_text']['und'][0]['value']['#value'] = 'No'; // set value to No 
-1

Use hook_node_insert instead of alter the form.

This hook is invoked from node_save() after the database query that will insert the node into the node table is scheduled for execution, after the type-specific hook_insert() is invoked, and after field_attach_insert() is called.

Note that when this hook is invoked, the changes have not yet been written to the database, because a database transaction is still in progress. The transaction is not finalized until the save operation is entirely completed and node_save() goes out of scope. You should not rely on data in the database at this time as it is not updated yet. You should also note that any write/update database queries executed from this hook are also not committed immediately. Check node_save() and db_transaction() for more info.

Example :

function modulename_node_insert($node) {
  // Just change the node object and value will be saved.
}

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.