admin管理员组

文章数量:1405132

I have rules that validate a group (requires one of 3). That works fine, however, I need to only have the 1 of 3 requirement depend on a selection 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 just returns true instead of the code I wrote, which is necessary for the require_from_group() function.

I have rules that validate a group (requires one of 3). That works fine, however, I need to only have the 1 of 3 requirement depend on a selection 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 just returns true instead of the code I wrote, which is necessary for the require_from_group() function.

Share Improve this question edited Feb 12, 2016 at 22:11 RPL asked Jul 7, 2014 at 20:29 RPLRPL 3,6472 gold badges22 silver badges28 bronze badges 4
  • it should require when PIC is selected or otherwise? cause looks like if else return statements are inversed – G.Mendes Commented Jul 7, 2014 at 20:46
  • if PIC is selected it should not require – RPL Commented Jul 7, 2014 at 20:47
  • Try actually removing the nested depends from require_from_group, it should work as require_from_group:[1, ".verification-group"], depends: function(element){[..]} – G.Mendes Commented Jul 7, 2014 at 20:57
  • That doesn't work, that will require from group at all times and the conditional doesn't take effect. – RPL Commented Jul 7, 2014 at 21:12
Add a ment  | 

2 Answers 2

Reset to default 4

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 .....}
       }
    },
}

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');
  }
}

本文标签: javascriptjQuery Validation Depends and Require from GroupStack Overflow