Skip to main content
5 of 7
added 988 characters in body

Webform element validation custom module

I want to add a custom validation to text input, I followed the instruction provided in https://www.drupal.org/docs/8/modules/webform/webform-cookbook/how-to-add-custom-validation-to-a-webform-element

I have a requirement which needs to verify an id entered into a text box, by making a REST API call to another system, the issue is to make that REST API call, I need another text parameter which is also inputed into the webform. I have written a custom handler which makes that rest call, but that custom handler work on webform post save option only, how can I pass more than 2 fields to the validate function mentioned in the example given.

function onboarding_webform_element_alter(array &$element, FormStateInterface $form_state, array $context) { // check for desired element
if (isset($element['#webform_id']) && $element['#webform_id'] === 'register_account--surname'){ $surname= $element['#default_value']; $form_state->setValue('SOME_NAME', $surname);
}
if (isset($element['#webform_id']) && $element['#webform_id'] === 'register_account--proda_id') {

    $element['#element_validate'][] = array(
        'Drupal\dhs_vendor_onboarding\Validate\DhsValidateConstraint',
        'validate'
    );      
} 

}

in the validate function public static function validate(array &$element, FormStateInterface $formState, array &$form) {

    $webformKey = $element['#webform_key'];
   
    $value = $formState->getValue($webformKey);
    $surname= $form_state->getValue('SOME_NAME');

} i am not getting any value for surname