I am attempting to modify form fields from a submit handler of a form. I do this by altering the image_node_form to add a submit button with a custom submit function, then calling form_set_value in that submit function and setting $form_state['redirect'] = FALSE.

But it does not work: Whatever was in the field before the submit button was clicked remains in the field.

Very obvious question: What am I doing wrong?

Thanks,
Kai

I verified this by installing Drupal 6.9 from scratch, then installing image-6.x-1.0-alpha4, then adding the following 20 line module. (*.info file skipped for brevity.)

function mumble_form_image_node_form_alter(&$form, $form_state)
{
    $form['mumble_do'] = array(
            '#type' => 'submit',
            '#value' => 'Do Stuff',
            '#submit' => array('mumble_do_submit'),
    );
    $form['title']['#default_value'] = 'mumble';
}

function mumble_do_submit($form, &$form_state)
{
    form_set_value($form['title'], 'unmumbled', $form_state);
    //$form_state['clicked_button']['#post']['title'] = 'unmumbled';
    $form_state['redirect'] = FALSE;
    //$form_state['rebuild'] = TRUE;
    //$form_state['storage']['mumble'] = 1;
}

Comments

nicksanta’s picture

hi, i haven't tried to do this specifically, but have you tried a validation handler instead? i've always found editing form values easier in the validation stage.

<?php
function your_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
  case 'your_form':
    $form['#validate'][] = 'your_form_validate';
    break;
  }
}

function your_form_alter($form, &$form_state) {
  $form_state['redirect'] = FALSE;
}

----------------------
Nick Santamaria

kaig’s picture

Thanks a lot for your comment.

I just made that one-word change from '#submit' to '#validate', and tried again, with the same result. Sigh.

(In my real code, I want to process the just-uploaded image file; I'm not sure if I can do that from a validation handler? Is the image file available at that point?)

apg’s picture

To change the title in a submit function:

function mumble_do_submit($form, &$form_state) {

$form_state['values']['title'] = check_plain('Welcome to my new title');
}