Skip to main content
Added some code comments for explanation.
Source Link
jordojuice
  • 1.2k
  • 9
  • 12

If you want to set a form value from within your validation callback you'll need to use something like form_set_value. Values that are assigned directly in the manner shown above will not be retained, but that function should do the trick! It's specifically intended for this purpose - setting a form value during the validation phase.

For example:

function myform_validate($form, &$form_state) {
  if (empty($form_state['values']['field_b']['0']['value']$form_state['values']['field_b'])) {
        // Note that the $form array - not $form_state - is used in the first parameter.
        // The second parameter is the value you are setting.
        // Third just pass the $form_state array itself, which will be altered by form_set_vaue().
        form_set_value($form['field_b'], $form_state['values']['field_a'], $form_state); 
      }
      if (strlen($form_state['values']['field_b']['0']['value']$form_state['values']['field_b']) > 260) {
        form_set_error('field_b', 'Text in field b is too long');
      }
    }
  }
}

If you want to set a form value from within your validation callback you'll need to use something like form_set_value. Values that are assigned directly in the manner shown above will not be retained, but that function should do the trick! It's specifically intended for this purpose - setting a form value during the validation phase.

For example:

function myform_validate($form, &$form_state) {
  if (empty($form_state['values']['field_b']['0']['value'])) {
        form_set_value($form['field_b'], $form_state['values']['field_a'], $form_state); 
      }
      if (strlen($form_state['values']['field_b']['0']['value']) > 260) {
        form_set_error('field_b', 'Text in field b is too long');
      }
    }
  }
}

If you want to set a form value from within your validation callback you'll need to use something like form_set_value. Values that are assigned directly in the manner shown above will not be retained, but that function should do the trick! It's specifically intended for this purpose - setting a form value during the validation phase.

For example:

function myform_validate($form, &$form_state) {
  if (empty($form_state['values']['field_b'])) {
        // Note that the $form array - not $form_state - is used in the first parameter.
        // The second parameter is the value you are setting.
        // Third just pass the $form_state array itself, which will be altered by form_set_vaue().
        form_set_value($form['field_b'], $form_state['values']['field_a'], $form_state); 
      }
      if (strlen($form_state['values']['field_b']) > 260) {
        form_set_error('field_b', 'Text in field b is too long');
      }
    }
  }
}
added 439 characters in body
Source Link
jordojuice
  • 1.2k
  • 9
  • 12

If you want to set a form value from within your validation callback you'll need to use something like form_set_value. Values that are assigned directly in the manner shown above will not be retained, but that function should do the trick! It's specifically intended for this purpose - setting a form value during the validation phase.

For example:

function myform_validate($form, &$form_state) {
  if (empty($form_state['values']['field_b']['0']['value'])) {
        form_set_value($form['field_b'], $form_state['values']['field_a'], $form_state); 
      }
      if (strlen($form_state['values']['field_b']['0']['value']) > 260) {
        form_set_error('field_b', 'Text in field b is too long');
      }
    }
  }
}

If you want to set a form value from within your validation callback you'll need to use something like form_set_value. Values that are assigned directly in the manner shown above will not be retained, but that function should do the trick! It's specifically intended for this purpose - setting a form value during the validation phase.

If you want to set a form value from within your validation callback you'll need to use something like form_set_value. Values that are assigned directly in the manner shown above will not be retained, but that function should do the trick! It's specifically intended for this purpose - setting a form value during the validation phase.

For example:

function myform_validate($form, &$form_state) {
  if (empty($form_state['values']['field_b']['0']['value'])) {
        form_set_value($form['field_b'], $form_state['values']['field_a'], $form_state); 
      }
      if (strlen($form_state['values']['field_b']['0']['value']) > 260) {
        form_set_error('field_b', 'Text in field b is too long');
      }
    }
  }
}
Source Link
jordojuice
  • 1.2k
  • 9
  • 12

If you want to set a form value from within your validation callback you'll need to use something like form_set_value. Values that are assigned directly in the manner shown above will not be retained, but that function should do the trick! It's specifically intended for this purpose - setting a form value during the validation phase.