JQuery validation depends on the requirements and requirements of the group

I have rules that validate a group (one of 3 is required). This works fine, however I only need 1 out of 3 requirements to depend on the choice in the form.

rules: {
  cloudfront_7: {
    require_from_group: {
      depends: function(element) {
        if ($( "#classify" ).val() == "PIC" ){
          return false;
        } else {
          return [1, ".verification-group"];
        }
      }
    }
  },
  cloudfront_8: {
    require_from_group: {
      depends: function(element) {
        if ($( "#classify" ).val() == "PIC" ){
          return false;
        } else {
          return [1, ".verification-group"];
        }
      }
    }
  },
  cloudfront_9: {
    require_from_group: {
      depends: function(element) {
        if ($( "#classify" ).val() == "PIC" ){
          return false;
        } else {
          return [1, ".verification-group"];
        }
      }
    }
  }
}

I noticed that it simply returns, trueinstead of the code I wrote, what is needed for the function require_from_group().

+4
source share
2 answers

I solved the problem using these custom rules:

var verDoc_required = {
  cloudfront_7: {
    require_from_group: [1, ".verification-group"]
  },
  cloudfront_8: {
    require_from_group: [1, ".verification-group"]
  },
  cloudfront_9: {
    require_from_group: [1, ".verification-group"]
  }
}

$("#classify").change(function() {
  if ( $(this).val() == "PIC" ) {
    removeRules(verDoc_required);
  } else {
    addRules(verDoc_required);
  }
});

function addRules(rulesObj) {
  for (var item in rulesObj) {
     $('#' + item).rules('add', rulesObj[item]);
  }
}
function removeRules(rulesObj) {
  for (var item in rulesObj) {
     $('#' + item ).rules('remove');
  }
}
+4
source

you can try this code:

rules: {
cloudfront_7: {
require_from_group: {
param: [1, "..verification-group"],
depends: function(element) {return .....}
       }
    },
cloudfront_8: {
require_from_group: {
param: [1, "..verification-group"],
depends: function(element) {return .....}
       }
    },
cloudfront_9: {
require_from_group: {
param: [1, "..verification-group"],
depends: function(element) {return .....}
       }
    },
}
+3
source

Source: https://habr.com/ru/post/1547489/


All Articles