I have created a textfield (length 2 char, mandatory) where a user is expected to enter a number between 0 (zero) and 99. If the user enters 0, an error is received saying that the field is mandatory, ie it does not accept 0 as a valid value. If the user enters 00, webform accepts the submission.

Comments

jnt’s picture

Yes, this is because the _webform_submission_ok function uses empty() which has the behaviour:

empty() returns FALSE if var has a non-empty and non-zero value.

This explains the behaviour you cite. This patch of the _webform_submission_ok function in webform.module would fix that:

function _webform_submission_ok($name, $type, $value, $mandatory) {
  // Check if it is mandatory and if so that it exists and have a value
- if($mandatory && empty($value)) {
+ if($mandatory && !strlen(trim($var1))) {
    form_set_error('submitted]['.$name, t('You have not completed the form. %s is a mandatory field.', array('%s' => $name)));
    return 1;
  }
  return 0;
}
jnt’s picture

Sorry, that should be $value in there, not $var1 which was my test variable: Here it is again corrected:

...
function _webform_submission_ok($name, $type, $value, $mandatory) {
  // Check if it is mandatory and if so that it exists and have a value
- if($mandatory && empty($value)) {
+ if($mandatory && !strlen(trim($value))) {
    form_set_error('submitted]['.$name, t('You have not completed the form. %s is a mandatory field.', array('%s' => $name)));
    return 1;
  }
  return 0;
}
...
GrayThunder’s picture

That took care of the "problem". Thanks for your help.

ullgren’s picture

Thanks jnt!
Patch commited to the CVS.

Lappie’s picture

It's not only the case that (without JNT's patch) webform doesn't accept 0's in textfields upon submitting, but it also doesn't accept 0's in select fields. In this case the behavior is even more problematic because the 0 in the list of select options just disappears on completing the filling in of the fields of a select-variable.

System info. Not a real CVS dude, so I manually downloaded the latest versions of the .inc, .mysql, and .module, installed, activated and updated.

1) create new webform.
2) add a select-field
3) enter a list of options (each on a new line): -2, -1, 0, 1, 2, 3
4) click done
5) preview/submit

Zero disappeared.

jnt’s picture

Yes, this appears to be a similar bug to the previous, caused by the numeric interpretations of the empty() function. Therefore a similar fix seems to work. This hack is not without consequences however, the zero option makes itself the default by default.

The change is to approximately line 744 of webform.module:

  case 'select':
    $items = explode("\n", _webform_filtervalues($extra['items']));
    foreach($items as $k => $v) {
      $v = trim($v);
-      if( !empty($v) )
+      if ( strlen($v) )
        $options[$v] = $v;
    }
    if ( $extra['multiple'] == 'Y' ) {
      if ( !is_array($value) ) {

This has had only very brief testing, so any feedback would be appreciated.

ullgren’s picture

Changes are now in cvs

ullgren’s picture