Not a very snappy forum title, but it's difficult to explain in one sentence! ;)

I have used webform to create a contact form on my customer's site. You can see it at
http://www.lucy-connelly.com/mmu/contact .

There are several select-type input fields - eg. Title: Mr | Mrs | Ms etc.

I don't want any particular value to be selected to start with, so I have added the option "-select-" at the top of my list of values, and this is what's displayed in the select before the user chooses anything.

I want the user to enter a value in this field, so I have made it mandatory. But of course this doesn't work, as "-select-" is also a value - the user can just leave the field set to "-select-" !

How can I have no option or a dummy option displayed to start with, and still make the user select a real option?

Comments

Caesar Tjalbo’s picture

The solution, thanks to Salvatored: http://drupal.org/node/71047

lucyconnuk’s picture

Thanks Caesar - I think this is probably the right track, but I'm not quite there yet.

I have added the workaround, and added a taxonomy category with Mr, Ms, Mrs etc in it.

But the category appears on the contact form edit page (of course) and not the contact form itself.

So I guess I should be adding a similar workaround somewhere in the webform code, rather than the taxonomy code? (I don't really understand drupal very well yet.) Any ideas?

Lucy C

pobster’s picture

I don't use webform myself, so this is a small amount of guesswork... But if you look at the form id that creates the form, it'll have a form_id_submit and a form_id_validate hook with it. You can then use the validate hook to create a form_error if the form data returns "-select-".

Should work...

Pobster

pobster’s picture

*cough* http://cvs.drupal.org/viewcvs/drupal/contributions/modules/webform/webfo...

/** 
 * Implementation of hook_validate().
 * This function is called after the user clicks any button displayed in webform_form().
 * Rather than a typical usage of validation, in webform this is used to perform various
 * actions without kicking the user out of the 'edit' tab. For instance, webform detects
 * the when the buttons "Add" or "Edit Selected" are clicked, and then direct the user
 * to a form for editing a component.
 **/
function webform_validate (&$node) {

Pobster

lucyconnuk’s picture

I confess I am confused by the whole thing, but isn't this function used when filling in the form to *edit* the webform, rather than when filling in the form that webform *produces*? (my brain hurts ;)

Lucy C

lucyconnuk’s picture

OK, I have done something nasty and probably naughty to fix this problem. In form.inc, function _form_validate, after

if ($elements['#required'] && empty($elements['#value']) && $elements['#value'] !== '0') {
form_error($elements, t('%name field is required.', array('%name' => $elements['#title'])));
}

instead of salvatored's lines I have put

      if ($elements['#type'] == 'select' && ( ($elements['#value']=='0') || ($elements['#value']=='-select-') ) ) {
		form_error($elements, t('%name field is required.', array('%name' => $elements['#title'])));
		}

- so if I have an option "-select-" in my select field, it won't be allowed.

A bit dirty but I have to be pragmatic!

Thanks for everyone's help.

Lucy C

pobster’s picture

There's nothing wrong with doing it that way... The only downside to it is that you'll not be able to have "-select-" as valid on any form_select items not just the webform, which I guess isn't a downside at all... As that'd never be a selection you'd want anyway? Sleep well knowing that your 'naughty' fix isn't so naughty after all! ;o)

Pobster

lammmy’s picture

This seems to work with the select lists, but for some reason no matter what I do, if I add a "select" component to my form, they are not being validated and even if nothing is checked, the form is allowed to be submitted...anything I'm missing here?

lucyconnuk’s picture

On some later testing I realised I had a couple of mistakes in this:

1) I left out the if($elements['required']) check, so it was checking all selects, even if they weren't mandatory
2) Complaining if an element value was 0 was unnecessary, as my selects have a value of "-select-". Checking for 0 was interfering with some other selects (eg. when trying to select "" as a forum parent.)

So I changed my edit to:

      if ($elements['#required'] && $elements['#type']=='select' && $elements['#value']=='-select-') {
		form_error($elements, t('%name field is required.', array('%name' => $elements['#title'])));
		}

Lucy C

marcoBauli’s picture

worked fine for me :) thanks!