Question

I have a webform, and a custom validation function. Which both work fine.

But when I do a form_set_value inside the validation function, nothing happens.

Can anyone help me out please

function test_webservices_validate(&$form, &$form_state) {

    if (form_get_errors())
        return;
    $form_values = $form_state['values']['submitted'];

    $Address = _test_webservices_translate($values);
    if (!$Address->succes) {
            form_set_value($form['submitted']['voornaam'], 'sdfdsfsd', $form_state);
            dpm($form);
            dpm($form_state);
    } else {
        _test_webservices_handledcm($form, $form_state, $Address);
    }
}
Was it helpful?

Solution 4

Ok,

What happens is when going trough the flow, somewhere in one of the functions the variables are not passed as a reference, so you actually loose the information you changed. I fixed this by changing core functionality and passing the object as a reference.

OTHER TIPS

Kindly refer to form_set_value() documentation on api.drupal.org.

They say

if you want to update the value of $form['elem1']['elem2'], which should be stored in $form_state['values']['elem1']['elem2'], you would set $element['#parents'] = array('elem1','elem2').

Hope that helps

Muhammad.

Can you give some more information? Should the value appear with ajax after validating? Also don't forget to add $form_state['rebuild'] = TRUE; at the bottom of your validation function. This will cause the $form array to rebuild based on the values in $form_state. The only thing that form_set_value does is update the $form_state array

Hmmm, another approach could be that you form alter your form and do three things: 1) add to your button(submit?):

$form['submit']['#limit_validation_errors'] = array();

2) override all validation functions with

$form['#validate'] = 'own_validation_function_callback'

3) check if custom valid form_state variable isset

if (isset($form_state['custom_not_valid'])) {
$form['extra_element'] = ... }

In your own_validation_function_callback you: validate your form yourself, maybe with drupal_validate_form() And if not valid you add to your form_state (if not try to unset it!!)

$form_state['custom_not_valid'] = TRUE;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top