Hi,
I have created 4-5 fieldsets,and all the fields are mandatory. i would like that if user chooses not to add further fieldsets he should be able to submit form and mandatory condition should be ignored in the hidden Fieldsets

Comments

webadpro’s picture

I'm actually facing the same issue at the moment.

matt b’s picture

I was thinking of using the webform validation to do some of this...

matt b’s picture

Version: 7.x-1.01 » 6.x-1.03

So, with the webform validation module installed, you can create additional validations by creating a custom module and implementing hooks from the webform validation module (see the readme.txt in the validation module)

I've created a validator called 'complete set' which lets you select a set of fields (the fields in your fieldset, for example) and validates that either all the fields in the set have been completed, or none of them have.

function mymodule_webform_validation_validators() {
  return array(
      'complete_set' => array(
      'name' => "Complete Set",
      'component_types' => array('all'),
      'description' => t("Validates that all the fields in the set are completed or that none of them are"),
    ),
  );
}

function mymodule_webform_validation_validate($validator_name, $items, $components, $rule) {
  if ($items) {
    $errors = array();
    switch ($validator_name) {
      case 'complete_set':
        foreach ($items as $key => $val) {
          if (is_array($val)) {
            // make sure to flatten arrays first
            $items[$key] = _webform_validation_flatten_array($val);
          }
        }
        // $components have no entries at all or should all be completed
        if ((count(array_filter($items)) != 0) && (count(array_filter($items))!=count($items))) {
          $keys = array_keys($items);
          $names = array();
          foreach ($keys as $value) {
            $names[] = $components[$value]['name'];
          }
          return array($keys[0] => t('You have to enter all of the following items (or none of them):') . theme('item_list', $names));
        }
        break;
     }
  }
}

It's a start, but maybe something needs to be done with a hidden field that could have a value set when the fieldset is displayed. If this hidden field was included in the 'complete set' validation then it would force the fields in the fieldset to be mandatory when the fieldset is displayed.

You need to create a 'complete set' validation rule for each fieldset (for all of the fields in the fieldset).

ongdesign’s picture

Thanks, Matt, this was really helpful. When I added a validation rule using a Date field, I did have to define a slightly different array flattening function to remove the commas, as they were throwing a false positive when a date value was unset. Instead of _webform_validation_flatten_array(), I just used

function _nocommas_flatten_array($val) {
  if (is_array($val)) {
    $arr = array_filter($val, '_webform_validation_check_false');
    return implode('', $arr);
  }
  return $val;
}

and the rest of your code worked great.

thentha18’s picture

thanks for the code

1 thing though, the error message is shown at the top but the fields are not highlighted

chertzog’s picture

Status: Active » Fixed

Closing as custom webform validators can handle this.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.