Hello,

I am writing a module and I want to update the form content during user input.
I want to update a textfield when the user click on a button.
I process button input in hook_nodeapi, but I cannot get the form updated.
I use form_set_value, there are no error and values are changed in the debugger, but the displayed form remains unchanged. What am I missing ?

function bs_document_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  switch($op) {
    case 'validate':
      // detect button
      if ($a3['#post']['op'] == BS_GALERIE_SELECT) {
        if (($gal = intval($a3['#post']['galerie_select'])) > 0) {
          $exposition = bs_galerie_adresse($gal);
          form_set_value($a3['grp_entete']['exposition'], $exposition);
        }
      }
      break;

Comments

rimian’s picture

Maybe a jQuery or javascript function would be required if it is during user input.

http://www.rimian.com.au

mooffie’s picture

values are changed in the debugger, but the displayed form remains unchanged

Form values are actually stored in two different places: in a global $form_values array, and in the '#value' slot of each element. When you use form_set_values() you affect only the $form_values array. But when the form is shown --when there's $_POST data-- it is the '#value' slots that are used.

flebas’s picture

When you use form_set_values() you affect only the $form_values array. But when the form is shown --when there's $_POST data-- it is the '#value' slots that are used.

How can I change the value of '#value' slot so the change is applied in the next form display ?
I have tried to change it in hook_nodeapi (and changed $a3 to &$a3), but it does not work.

function bs_test_nodeapi(&$node, $op, &$a3 = NULL, $a4 = NULL) {

  switch($op) {
    case 'validate':
      if ($node->type == 'bs_test') {

        // $a3 = $form
        if ($a3['#post']['op'] == 'My button') {
          $new_value = 'Button clicked';
          form_set_value($a3['texte'], $new_value);
          $a3['texte']['#value'] = $new_value;

mooffie’s picture

There are several ways to do this. I don't know what exactly your scenario is. One way is to add a #after_build handler to the form:

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

and in it:

function mymodule_button_handler($form, $form_values) {
  if ($_POST['op'] == 'my button') {
    $form['one']['#value'] = $form_values['another'] * 2;
  }
  return $form;
}
flebas’s picture

It works !

The #after_build should be added in hook_form_alter, if set in hook_form, it is overwritten during form build.

Thank you very much !

I could not imagine this was not possible with Drupal and did not find any way to do this.