Skip to content

Commit

Permalink
Fixing CountStrategy to only apply to live validation fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mtscout6 committed Mar 6, 2014
1 parent 71a35a9 commit bffc0e0
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 22 deletions.
38 changes: 24 additions & 14 deletions src/FubuMVC.Validation/content/scripts/fubuvalidation.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,7 @@
}
});
},
init: function(element, target, rules) {
var context = {
element: element,
target: target,
rules: rules
};

init: function(context) {
_.each(this.strategies, function (strategy) {
if (_.isFunction(strategy.initMatches) &&
strategy.initMatches(context) &&
Expand Down Expand Up @@ -316,10 +310,21 @@

CountStrategy.prototype = {
initMatches: function (context) {
return context.rules !== undefined && context.rules.length > 0;
var field = context.target.fieldName;

return _.any(context.options.fields, function(x) {
return x.field === field &&
x.mode === 'live' &&
((x.rules && x.rules.length > 0) ||
(context.plan && context.plan.runners && context.plan.runners.length > 0));
});
},
matches: function (context) {
return true;
if (!context.element) {
return false;
}

return context.element.is('[' + this.dataKey + ']');
},
init: function(context) {
context.element.attr(this.dataKey, 0);
Expand Down Expand Up @@ -449,11 +454,16 @@
var mode = validation.Core.ValidationMode.Live;
var initSelector = "input:not(:submit,:reset,:image,[disabled]),textarea:not([disabled])";
var init = function() {
var element = $(this)
, target = validation.Core.Target.forElement(element, form.attr('id'), form)
, rules = self.validator.rulesFor(target);

self.processor.handler.init(element, target, rules);
var element = $(this),
target = validation.Core.Target.forElement(element, form.attr('id'), form),
context = {
element: element,
target: target,
options: options,
plan: self.validator.planFor(target, options, validation.Core.ValidationMode.Live)
};

self.processor.handler.init(context);
};

form.find(initSelector).each(init);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -966,30 +966,105 @@ describe('CountStrategy tests', function() {
theStrategy = new $.fubuvalidation.UI.Strategies.Count();
});

it('does not match init when context has no rules defined', function() {
var context = { };
it('does not match init when context has no fields defined', function() {
var context = {
target: {
fieldName: 'TestName'
},
options: {
fields: []
}
};

expect(theStrategy.initMatches(context)).toEqual(false);
});

it('does not match init when context does not define options for field of interest', function() {
var context = {
target: {
fieldName: 'TestName'
},
options: {
fields: [{
field: 'OtherField'
}]
}
};

expect(theStrategy.initMatches(context)).toEqual(false);
});

it('does not match init field is triggered validation', function() {
var context = {
target: {
fieldName: 'TestName'
},
options: {
fields: [{
field: 'TestName',
mode: 'triggered'
}]
}
};

expect(theStrategy.initMatches(context)).toEqual(false);
});

it('does not match init when context has no rules', function() {
it('does not match init when field is live validation with no plan runners', function() {
var context = {
rules: []
target: {
fieldName: 'TestName'
},
options: {
fields: [{
field: 'TestName',
mode: 'live',
rules: []
}]
}
};

expect(theStrategy.initMatches(context)).toEqual(false);
});

it('matches init when context has at least one rule', function() {
it('matches init when field is live validation with plan runners', function() {
var context = {
rules: [{}]
target: {
fieldName: 'TestName'
},
options: {
fields: [{
field: 'TestName',
mode: 'live'
}]
},
plan: {
runners: [{}]
}
};

expect(theStrategy.initMatches(context)).toEqual(true);
});

it('matches all the time', function() {
expect(theStrategy.matches()).toEqual(true);
it('does not match when no element provided', function() {
var context = { };
expect(theStrategy.matches(context)).toEqual(false);
});

it('does not match when element does not have count attr', function() {
var context = {
element: $('<input type="text" />')
};

expect(theStrategy.matches(context)).toEqual(false);
});

it('matches when element has count attr', function() {
var context = {
element: $('<input type="text" data-validation-count="0" />')
};

expect(theStrategy.matches(context)).toEqual(true);
});

it('initializes a default validation count of zero', function() {
Expand Down

0 comments on commit bffc0e0

Please sign in to comment.