mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-14 17:53:11 +00:00
Fix bug with validator not triggering when attributes are bound and fix some typos. Add test for bug
This commit is contained in:
parent
bbb45a7eed
commit
769b26b79e
3 changed files with 22 additions and 11 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
foreach({
|
foreach({
|
||||||
'noop': noop,
|
'noop': function() { return null; },
|
||||||
|
|
||||||
'regexp': function(value, regexp, msg) {
|
'regexp': function(value, regexp, msg) {
|
||||||
if (!value.match(regexp)) {
|
if (!value.match(regexp)) {
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,9 @@ function modelAccessor(scope, element) {
|
||||||
|
|
||||||
function modelFormattedAccessor(scope, element) {
|
function modelFormattedAccessor(scope, element) {
|
||||||
var accessor = modelAccessor(scope, element),
|
var accessor = modelAccessor(scope, element),
|
||||||
farmatterName = element.attr('ng-format') || NOOP,
|
formatterName = element.attr('ng-format') || NOOP,
|
||||||
formatter = angularFormatter(farmatterName);
|
formatter = angularFormatter(formatterName);
|
||||||
if (!formatter) throw "Formatter named '" + farmatterName + "' not found.";
|
if (!formatter) throw "Formatter named '" + formatterName + "' not found.";
|
||||||
return {
|
return {
|
||||||
get: function() {
|
get: function() {
|
||||||
return formatter.format(accessor.get());
|
return formatter.format(accessor.get());
|
||||||
|
|
@ -36,12 +36,12 @@ function valueAccessor(scope, element) {
|
||||||
var validatorName = element.attr('ng-validate') || NOOP,
|
var validatorName = element.attr('ng-validate') || NOOP,
|
||||||
validator = compileValidator(validatorName),
|
validator = compileValidator(validatorName),
|
||||||
requiredExpr = element.attr('ng-required'),
|
requiredExpr = element.attr('ng-required'),
|
||||||
farmatterName = element.attr('ng-format') || NOOP,
|
formatterName = element.attr('ng-format') || NOOP,
|
||||||
formatter = angularFormatter(farmatterName),
|
formatter = angularFormatter(formatterName),
|
||||||
format, parse, lastError, required;
|
format, parse, lastError, required;
|
||||||
invalidWidgets = scope.$invalidWidgets || {markValid:noop, markInvalid:noop};
|
invalidWidgets = scope.$invalidWidgets || {markValid:noop, markInvalid:noop};
|
||||||
if (!validator) throw "Validator named '" + validatorName + "' not found.";
|
if (!validator) throw "Validator named '" + validatorName + "' not found.";
|
||||||
if (!formatter) throw "Formatter named '" + farmatterName + "' not found.";
|
if (!formatter) throw "Formatter named '" + formatterName + "' not found.";
|
||||||
format = formatter.format;
|
format = formatter.format;
|
||||||
parse = formatter.parse;
|
parse = formatter.parse;
|
||||||
if (requiredExpr) {
|
if (requiredExpr) {
|
||||||
|
|
@ -86,8 +86,8 @@ function valueAccessor(scope, element) {
|
||||||
var error,
|
var error,
|
||||||
validateScope = extend(new (extend(function(){}, {prototype:scope}))(), {$element:element});
|
validateScope = extend(new (extend(function(){}, {prototype:scope}))(), {$element:element});
|
||||||
error = required && !value ?
|
error = required && !value ?
|
||||||
"Required" :
|
'Required' :
|
||||||
(value ? validator(validateScope, value) : null);
|
value ? validator(validateScope, value) : null;
|
||||||
elementError(element, NG_VALIDATION_ERROR, error);
|
elementError(element, NG_VALIDATION_ERROR, error);
|
||||||
lastError = error;
|
lastError = error;
|
||||||
if (error) {
|
if (error) {
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ describe("widget", function(){
|
||||||
|
|
||||||
describe("ng-format", function(){
|
describe("ng-format", function(){
|
||||||
|
|
||||||
it("should farmat text", function(){
|
it("should format text", function(){
|
||||||
compile('<input type="Text" name="list" value="a,b,c" ng-format="list"/>');
|
compile('<input type="Text" name="list" value="a,b,c" ng-format="list"/>');
|
||||||
expect(scope.$get('list')).toEqual(['a', 'b', 'c']);
|
expect(scope.$get('list')).toEqual(['a', 'b', 'c']);
|
||||||
|
|
||||||
|
|
@ -178,7 +178,18 @@ describe("widget", function(){
|
||||||
expect(element.attr('ng-validation-error')).toEqual('Not a number');
|
expect(element.attr('ng-validation-error')).toEqual('Not a number');
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should not call validator if undefinde/empty", function(){
|
it('should not blow up for validation with bound attributes', function() {
|
||||||
|
compile('<input type="text" name="price" boo="{{abc}}" ng-required/>');
|
||||||
|
expect(element.hasClass('ng-validation-error')).toBeTruthy();
|
||||||
|
expect(element.attr('ng-validation-error')).toEqual('Required');
|
||||||
|
|
||||||
|
scope.$set('price', '123');
|
||||||
|
scope.$eval();
|
||||||
|
expect(element.hasClass('ng-validation-error')).toBeFalsy();
|
||||||
|
expect(element.attr('ng-validation-error')).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not call validator if undefined/empty", function(){
|
||||||
var lastValue = "NOT_CALLED";
|
var lastValue = "NOT_CALLED";
|
||||||
angularValidator.myValidator = function(value){lastValue = value;};
|
angularValidator.myValidator = function(value){lastValue = value;};
|
||||||
compile('<input type="text" name="url" ng-validate="myValidator"/>');
|
compile('<input type="text" name="url" ng-validate="myValidator"/>');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue