Skip to content

Commit

Permalink
Merge pull request #252 from Availity/feature/avValShow-on-form
Browse files Browse the repository at this point in the history
allows fields to get av-val-show from the form
  • Loading branch information
robmcguinness committed Dec 8, 2016
2 parents 326f253 + 600ab2c commit 88bfe52
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/ui/validation/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
var avValField = controllers[2];

var avValOn = scope.avValOn || avValForm.avValOn || 'input';
scope.avValShow = scope.avValShow || avValForm.avValShow || null;

if(!ngModel && !rule) {
$log.error('avValField requires ngModel and a validation rule to run.');
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/validation/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,11 @@
});

// Allow form attributes to define the validation behavior of the form fields
// inside it. If `av-val-on` or `av-val-debounce` are on the form then all form
// inside it. If `av-val-on`, `av-val-debounce`, or `av-val-show` are on the form then all form
// fields inside the form would inherit this behavior.
avForm.avValOn = iAttrs.avValOn || null;
avForm.avValDebounce = iAttrs.avValDebounce || null;
avForm.avValShow = scope.$eval(iAttrs.avValShow) || null;
// Allows fields to update with invalid data for dirty form saving
avForm.avValInvalid = iAttrs.avValInvalid || false;

Expand Down
3 changes: 1 addition & 2 deletions lib/ui/validation/tests/field-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ describe('avValField', function() {
});

it('should have .has-error class on form group when options.show === true', function() {
availity.mock.$scope.myForm.showOnLoad.$setViewValue('1');
availity.mock.$scope.$digest();

var formGroup = $('#showOnLoadFormGroup');
expect(availity.mock.$scope.myForm.invalidAllowed.$invalid).toBe(true);
expect(availity.mock.$scope.myForm.showOnLoad.$invalid).toBe(true);
expect(formGroup.hasClass('has-error')).toBeTruthy();
});

Expand Down
53 changes: 53 additions & 0 deletions lib/ui/validation/tests/form-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,59 @@ describe('avForm', function() {
expect(availity.mock.$scope.demo.invalidAllowed).toBe('1');
});

it('should immediately create an error for firstName', function() {
var template = '' +
'<form name="myForm" ng-submit="submit()" data-av-val-form="demo.rules" data-av-val-show="true">' +
'<div id="showOnLoadFormGroup" class="form-group">' +
'<input data-ng-model="demo.showOnLoad" name="showOnLoad" type="text" data-av-val-field="lastName"/>' +
'<p data-av-val-container></p>' +
'</div>' +
'</form>';

$el = availity.mock.compileDirective(template);
availity.mock.$scope.$digest();

var formGroup = $('#showOnLoadFormGroup');
expect(availity.mock.$scope.myForm.showOnLoad.$invalid).toBe(true);
expect(formGroup.hasClass('has-error')).toBeTruthy();
});

it('should not immediately create an error if avValShow variable is false', function() {
var template = '' +
'<form name="myForm" ng-submit="submit()" data-av-val-form="demo.rules" data-av-val-show="demo.showError">' +
'<div id="showOnLoadFormGroup" class="form-group">' +
'<input data-ng-model="demo.showOnLoad" name="showOnLoad" type="text" data-av-val-field="lastName"/>' +
'<p data-av-val-container></p>' +
'</div>' +
'</form>';

availity.mock.$scope.demo.showError = false;

$el = availity.mock.compileDirective(template);
availity.mock.$scope.$digest();

var formGroup = $('#showOnLoadFormGroup');
expect(formGroup.hasClass('has-error')).toBeFalsy();
});

it('should immediately create an error if avValShow variable is true', function() {
var template = '' +
'<form name="myForm" ng-submit="submit()" data-av-val-form="demo.rules" data-av-val-show="demo.showError">' +
'<div id="showOnLoadFormGroup" class="form-group">' +
'<input data-ng-model="demo.showOnLoad" name="showOnLoad" type="text" data-av-val-field="lastName"/>' +
'<p data-av-val-container></p>' +
'</div>' +
'</form>';

availity.mock.$scope.demo.showError = true;

$el = availity.mock.compileDirective(template);
availity.mock.$scope.$digest();

var formGroup = $('#showOnLoadFormGroup');
expect(formGroup.hasClass('has-error')).toBeTruthy();
});

describe('submit', function() {

it('should prevent default action', function() {
Expand Down

0 comments on commit 88bfe52

Please sign in to comment.