mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-24 03:55:49 +08:00
fix(input): improve html5 validation support
This CL improves mocking support for HTML5 validation, fixes the behaviour which invokes validators. Previously, an input would only be revalidated if either its value changed, or if it was the empty string but did not suffer from bad input --- now, it will be revalidated if either the value has changed, or the value is the empty string, there is a ValidityState for the element, and that ValidityState is being tested by one of the validators in the pipeline. Closes #7937 Closes #7957
This commit is contained in:
@@ -410,15 +410,33 @@ describe('ngModel', function() {
|
||||
|
||||
|
||||
describe('input', function() {
|
||||
var formElm, inputElm, scope, $compile, $sniffer, $browser, changeInputValueTo;
|
||||
var formElm, inputElm, scope, $compile, $sniffer, $browser, changeInputValueTo, currentSpec;
|
||||
|
||||
function compileInput(inputHtml) {
|
||||
function compileInput(inputHtml, mockValidity) {
|
||||
inputElm = jqLite(inputHtml);
|
||||
if (isObject(mockValidity)) {
|
||||
VALIDITY_STATE_PROPERTY = 'ngMockValidity';
|
||||
inputElm.prop(VALIDITY_STATE_PROPERTY, mockValidity);
|
||||
currentSpec.after(function() {
|
||||
VALIDITY_STATE_PROPERTY = 'validity';
|
||||
});
|
||||
}
|
||||
formElm = jqLite('<form name="form"></form>');
|
||||
formElm.append(inputElm);
|
||||
$compile(formElm)(scope);
|
||||
}
|
||||
|
||||
var attrs;
|
||||
beforeEach(function() { currentSpec = this; });
|
||||
afterEach(function() { currentSpec = null; });
|
||||
beforeEach(module(function($compileProvider) {
|
||||
$compileProvider.directive('attrCapture', function() {
|
||||
return function(scope, element, $attrs) {
|
||||
attrs = $attrs;
|
||||
};
|
||||
});
|
||||
}));
|
||||
|
||||
beforeEach(inject(function($injector, _$sniffer_, _$browser_) {
|
||||
$sniffer = _$sniffer_;
|
||||
$browser = _$browser_;
|
||||
@@ -844,6 +862,33 @@ describe('input', function() {
|
||||
});
|
||||
|
||||
|
||||
it('should invalidate number if suffering from bad input', function() {
|
||||
compileInput('<input type="number" ng-model="age" />', {
|
||||
valid: false,
|
||||
badInput: true
|
||||
});
|
||||
|
||||
changeInputValueTo('10a');
|
||||
expect(scope.age).toBeUndefined();
|
||||
expect(inputElm).toBeInvalid();
|
||||
});
|
||||
|
||||
|
||||
it('should validate number if transition from bad input to empty string', function() {
|
||||
var validity = {
|
||||
valid: false,
|
||||
badInput: true
|
||||
};
|
||||
compileInput('<input type="number" ng-model="age" />', validity);
|
||||
changeInputValueTo('10a');
|
||||
validity.badInput = false;
|
||||
validity.valid = true;
|
||||
changeInputValueTo('');
|
||||
expect(scope.age).toBeNull();
|
||||
expect(inputElm).toBeValid();
|
||||
});
|
||||
|
||||
|
||||
describe('min', function() {
|
||||
|
||||
it('should validate', function() {
|
||||
|
||||
Reference in New Issue
Block a user