I'm sorry if this issue is covered somewhere, and I've just not been able to find it. I've been asking in irc and searching this site and have not found any indication that is or is not possible to have select options with values different than the text displayed by the option. For example, I have several forms where I need to list people in a select list menu, but I'd like to have that select option save their email address instead of their name in the form submission. Is this at all possible? If it's not possible, I'll submit a feature request, because I find it extremely difficult to believe I'm the only person in the world who wants to do this. If it is possible, and it's not documented anywhere, just tell me where I can document the process. I'm new to drupal, but I'd be glad to help out and make it easier for other people to find out how to do this.

Comments

davemybes’s picture

Actually it is possible, and it is documented. Written beneath the options box for a select object is the following:

Key-value pairs may be entered seperated by pipes. i.e. safe_key|Some readable option

However, after some testing, if you have any non-alphanumeric character in the key name e.g. a fullstop (.) or an @ symbol, it assumes that the line is your value.

So, basically, the functionality is there, it just won't do what you want it to :) My suggestion is to put some code into a template file called webform_form_#nodeid#.tpl.php (change #nodeid# to the node id of your webform), that finds that select box, and redefine the options using the forms API. Your code might look something like this:

$form['submitted']['option_test'] = array(  // change option_test to your field's key
  '#options' => array(
      'john@home.com' => t('John'),
      'mark@home.com' => t('Mark'), 
  ),
);

I haven't tested this to see if it works, but give it try.

quicksketch’s picture

This is also a very recent change. So it's not in 1.2 but will make it into the next stable release. The dev version is a little buggy atm, but you can just grab the select.inc component out of the dev version and you'll get this feature for the time being.

safetypin’s picture

Ah, I can't remember now, but I kept trying (and at some point forgot why I kept trying it) to use pipes to specify the values. Maybe it was from irc, which would explain how I didn't know that it needed to be a safe_key.

quicksketch: Do you mean that the pipes thing is not included in 1.2, or is there another, better way to specify values that is included in the dev version?

quicksketch’s picture

Right, the pipes are not available in the 1.2 version. You'll need to download the development version (http://drupal.org/project/webform) and grab the select.inc file from there until 1.3 is released.

safetypin’s picture

Well, I guess since the keys I want to use are not "safe", the pipe function is not really useful to me. I'm trying to take incrn8's advice, but I don't think I know enough about PHP to debug it. But, it sounds like that's more of what I want to do.

Just so I'm clear on how it's supposed to work, once I submit the form, the template "webform_form_#nodeid#.tpl.php" is executed, and replaces the selected Name with the corresponding Email address?

davemybes’s picture

Actually, the tpl.php file overrides the form values when you view the form i.e. before you even submit it. If you have this form available on the net, send me the link and I can create the tpl.php file for you if you like. If its not, then save the webpage with the form and email me the html file (via my contact page).

davemybes’s picture

Well, it turns out that my great idea to override the webform select options with a theme file only half works. The options are easily changed so that their key values are the required email addresses. However, when you try and submit the form, it fails and says that the selected option is an invalid choice. After a bit of fiddling, I think that webform is somehow validating the selected option against the original option, and not the modified one. When it sees that the two key values are not the same, it throws up the error. If I change the email address back to the value that Webform had created, then it goes through. This was tested on the 5.x-1.2 version.

Using the 5.x-1.x-dev version, I can use the pipes, but an email address is not valid it seems - neither the @ or . are allowed. If they are there, then the entire line is shown as the option. So, for this to work, an extra check to see if the key is an email address is required. The HTML specification does appear to allow those characters, so its something to do with webform's processing.

safetypin’s picture

Is there a way for me to override the validation of what defines a "safe_key"? I don't know very much PHP, but sometimes I know enough to hack something up to the point of working.

quicksketch’s picture

Actually, I think the current method is too strict. The only thing that really needs to be stripped out are quotes (or maybe they just need to be escaped). I'll investigate.

quicksketch’s picture

Version: 5.x-1.2 » 5.x-1.3

@matt.c I've refactored the "safe" values quite a bit in 1.3. Now you can use any characters besides spaces and quotes in the manually defined keys.

quicksketch’s picture

Status: Active » Fixed
Anonymous’s picture

Status: Fixed » Closed (fixed)
vacilando’s picture

Version: 5.x-1.3 » 5.x-2.1.3
Category: support » bug
Priority: Normal » Critical
Status: Closed (fixed) » Active

Subscribing...

vacilando’s picture

Category: bug » support
Priority: Critical » Normal
Status: Active » Closed (fixed)