0

I'm trying to set the selected value of a webform select form element before it is rendered. I'm using the below code and the textfields are working just fine but no luck with the select options.

function mytheme_preprocess_webform_form(&$vars) {
    $vars['form']['submitted']['emailAddress']['#value'] = 'some@email.com'; //This is working
    $vars['form']['submitted']['country']['#selected'] = 'BARBADOS'; // This is not working even though #default value is used instead
}

I can't figure it out why this is not working for select field. I would be glad if someone could help me.

Thanks in advance.

1

You can set value by using #value

$vars['form']['submitted']['country']['#value'] = 'BARBADOS';

But make sure that the select option key should be same

Example:

<option value="BARBADOS">BARBADOS</option>
$vars['form']['submitted']['country']['#value'] = 'BARBADOS'; //will work

<option value="barbados">BARBADOS</option>
$vars['form']['submitted']['country']['#value'] = 'BARBADOS'; //will not work
$vars['form']['submitted']['country']['#value'] = 'barbados'; //will work
  • Thanks man, I've spent hours for this, didn't expect it would be that easy. – Onur Kucukkece Aug 11 '17 at 11:08
  • Always welcome :) – PraveenKumar Aug 11 '17 at 11:09
0

For webform forms, I think you have to set #value to TRUE on the option itself.

Something like this should work:

$vars['form']['submitted']['country']['BARBADOS']['#value'] = TRUE;

If there are any other default values, you'll have to set those to FALSE.

  • Thanks a lot but nope, didn't work either – Onur Kucukkece Aug 9 '17 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.