Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 556f305

Browse files
committed
wip: use native max min validators
1 parent c0f37d6 commit 556f305

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

src/ng/directive/input.js

+40-26
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,10 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
14851485
badInputChecker(scope, element, attr, ctrl);
14861486
baseInputType(scope, element, attr, ctrl, $sniffer, $browser);
14871487

1488-
var minVal = 0, maxVal = 100;
1488+
var minVal = 0,
1489+
maxVal = 100,
1490+
hasNativeValidation = ctrl.$$hasNativeValidators && element[0].validity,
1491+
validity = element[0].validity;
14891492

14901493
ctrl.$$parserName = 'number';
14911494
ctrl.$parsers.push(function(value) {
@@ -1514,10 +1517,15 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15141517
}
15151518

15161519
if (isDefined(attr.min) || attr.ngMin) {
1517-
ctrl.$validators.min = function(value) {
1518-
return ctrl.$isEmpty(value) || isUndefined(minVal) || value >= minVal;
1519-
};
1520+
ctrl.$validators.min = hasNativeValidation && isDefined(validity.rangeUnderflow) ?
1521+
function nativeMaxValidator(value) {
1522+
return !element[0].validity.rangeOverflow;
1523+
} :
1524+
function maxValidator(modelValue, viewValue) {
1525+
return ctrl.$isEmpty(viewValue) || isUndefined(maxVal) || viewValue >= minVal;
1526+
};
15201527

1528+
// Assign minVal when the directive is linked. This won't $validate as the model isn't ready yet
15211529
minChange(attr.min);
15221530
attr.$observe('min', minChange);
15231531
}
@@ -1532,40 +1540,46 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15321540
}
15331541

15341542
if (isDefined(attr.max) || attr.ngMax) {
1535-
ctrl.$validators.max = ctrl.$$hasNativeValidators && isObject(element[0].validity) ?
1536-
function(value) {
1543+
ctrl.$validators.max = hasNativeValidation && isDefined(validity.rangeOverflow)?
1544+
function nativeMaxValidator(value) {
15371545
return !element[0].validity.rangeOverflow;
15381546
} :
1539-
function(modelValue, viewValue) {
1547+
function maxValidator(modelValue, viewValue) {
15401548
return ctrl.$isEmpty(viewValue) || isUndefined(maxVal) || viewValue <= maxVal;
15411549
};
15421550

15431551
console.log('max initial', attr.max);
1552+
// Assign maxVal when the directive is linked. This won't $validate as the model isn't ready yet
15441553
maxChange(attr.max);
15451554
attr.$observe('max', maxChange);
15461555
}
15471556

15481557
var originalRender = ctrl.$render;
15491558

1550-
ctrl.$render = function() {
1551-
if (ctrl.$isEmpty(ctrl.$viewValue)) {
1552-
// Browsers that implement range do this automatically, but IE9 does not
1553-
var viewValue = minVal + (maxVal - minVal) / 2;
1554-
ctrl.$viewValue = viewValue;
1555-
originalRender();
1556-
ctrl.$setViewValue(viewValue);
1557-
} else if (ctrl.$viewValue > maxVal) {
1558-
ctrl.$viewValue = maxVal;
1559-
originalRender();
1560-
ctrl.$setViewValue(ctrl.$viewValue);
1561-
} else if (ctrl.$viewValue < minVal) {
1562-
ctrl.$viewValue = minVal;
1563-
originalRender();
1564-
ctrl.$setViewValue(ctrl.$viewValue);
1565-
} else {
1566-
originalRender();
1567-
}
1568-
};
1559+
ctrl.$render = hasNativeValidation && isDefined(validity.rangeUnderflow) && isDefined(validity.rangeOverflow) ?
1560+
//Browsers that implement range will set these values automatically, but reading the adjusted values after
1561+
//$render would cause the min / max validators to be applied with the wrong value
1562+
function rangeRender() {
1563+
console.log('rangeRendr');
1564+
if (ctrl.$isEmpty(ctrl.$viewValue)) {
1565+
var viewValue = minVal + (maxVal - minVal) / 2;
1566+
ctrl.$viewValue = viewValue;
1567+
originalRender();
1568+
ctrl.$setViewValue(viewValue);
1569+
console.log('elval', element.val());
1570+
} else if (ctrl.$viewValue > maxVal) {
1571+
ctrl.$viewValue = maxVal;
1572+
originalRender();
1573+
ctrl.$setViewValue(ctrl.$viewValue);
1574+
} else if (ctrl.$viewValue < minVal) {
1575+
ctrl.$viewValue = minVal;
1576+
originalRender();
1577+
ctrl.$setViewValue(ctrl.$viewValue);
1578+
} else {
1579+
originalRender();
1580+
}
1581+
} :
1582+
originalRender;
15691583

15701584
}
15711585

0 commit comments

Comments
 (0)