Last active
December 15, 2015 00:19
-
-
Save jeremiaheb/5172134 to your computer and use it in GitHub Desktop.
Validation --- Overlapping Size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check that a species record does not have overlapping sizes with another | |
// record of the same species | |
//get current species record info....this is the species info to compare with all the others | |
$.validator.addMethod("doesNotHaveOverlap", function(value, element, params){ | |
var spp = {} | |
var $index = $(element).parent().parent().index(); | |
var $thisSpecies = $(element).parent().find('.sppCommon').select2("val"); | |
var $thisNumber = $(element).parent().find('[id$="number_individuals"]').val(); | |
var $thisMean = $(element).parent().find('[id$="average_length"]').val(); | |
var $thisMin = $(element).parent().find('[id$="min_length"]').val(); | |
var $thisMax = $(element).parent().find('[id$="max_length"]').val(); | |
var $thisRange | |
if ( $thisNumber == 1 ){ | |
$thisRange = [parseFloat($thisMean)]; | |
} else if ( $thisNumber == 2 ) { | |
$thisRange = [parseFloat($thisMin), parseFloat($thisMax)]; | |
} else { | |
$thisRange = _.range(parseFloat($thisMin), parseFloat($thisMax) + 1); | |
} | |
//get all other species entered thus far (are visible, same species code and not the record we are comparing with) and | |
//put in a multidimensional array | |
$("#animals .fields").each(function(i){ | |
if ($(this).is(":visible") && $(this).find(".sppCommon").select2("val") == $thisSpecies && i != $index ) { | |
var $animal = $(this).find('.sppCommon').select2("val"); | |
var $number = $(this).find('[id$="number_individuals"]').val(); | |
var $mean = $(this).find('[id$="average_length"]').val(); | |
var $min = $(this).find('[id$="min_length"]').val(); | |
var $max = $(this).find('[id$="max_length"]').val(); | |
var $range | |
if ( $number == 1 ){ | |
$range = [parseFloat($mean)]; | |
} else if ( $number == 2 ) { | |
$range = [parseFloat($min), parseFloat($max)]; | |
} else { | |
$range = _.range(parseFloat($min), parseFloat($max) + 1); | |
} | |
spp[i] = { "species": $animal, "range": $range}; | |
}; | |
}); | |
//if {spp} is not empty then loop through each record and test for overlapping ranges... | |
//trigger error if species sizes overlap with another record of same species | |
var hasOverlap; | |
if ( _.isEmpty(spp) ) { hasOverlap = false; } | |
else { | |
var checkBool = [] | |
for ( rec in spp ) { checkBool.push( _.intersection(spp[rec].range, $thisRange).length>0); }; | |
hasOverlap = _.contains( checkBool, true ); | |
} | |
console.log(spp); | |
console.log(checkBool); | |
return hasOverlap == false; | |
}, "record overlaps with other record" | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment