Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

Commit

Permalink
feat(core): Support form groups with multiple levels
Browse files Browse the repository at this point in the history
- add the message to the form group instead of directly after the form element if there is a form-group before the input field
  • Loading branch information
philippd committed Jun 2, 2014
1 parent 06fecd7 commit c7883ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/message/valdrMessage-directive.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/**
* This directive appends a validation message after each input or select element, which is nested in a valdr-type
* directive and has an ng-model bound to it.
* This directive appends a validation message to the parent element of any input, select or textarea element, which
* is nested in a valdr-type directive and has an ng-model bound to it.
* If the form element is wrapped in an element marked with the class defined in valdrClasses.formGroup,
* the messages is appended to this element instead of the direct parent.
* To prevent adding messages to specific input fields, the attribute 'no-valdr-message' can be added to those input
* or select fields. The valdr-message directive is used to do the actual rendering of the violation messages.
*/
var valdrMessageDirectiveDefinition = ['$compile', function ($compile) {
var valdrMessageDirectiveDefinition = ['$compile', 'valdrUtil', function ($compile, valdrUtil) {
return {
restrict: 'E',
require: ['?^valdrType', '?^ngModel', '?^form'],
Expand All @@ -25,7 +27,8 @@ var valdrMessageDirectiveDefinition = ['$compile', function ($compile) {
if (angular.isUndefined(attrs.noValdrMessage)) {
var formField = formController.$name + '.' + fieldName;
var valdrMessageElement = angular.element('<span valdr-message="' + formField + '"></span>');
element.after(valdrMessageElement);
var formGroup = valdrUtil.findWrappingFormGroup(element);
formGroup.append(valdrMessageElement);
$compile(valdrMessageElement)(scope);
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/message/valdrMessage-directive.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ describe('valdrMessage input directive', function () {
expect(nextElement.attributes['valdr-message'].value).toBe('demoForm.fieldName');
});


it('should add a the valdr-message directive to the form group if there is one', function () {
// given
var element = compileTemplate(
'<form name="demoForm">' +
'<section class="form-group">' +
'<div valdr-type="TestClass">' +
'<input type="text" name="fieldName" ng-model="myObject.field">' +
'</div>' +
'</section>' +
'</form>'
);

// when
var formGroupChildren = element.find('section').children();
var lastInFormGroup = formGroupChildren[formGroupChildren.length - 1];

//then
expect(lastInFormGroup.attributes['valdr-message']).toBeDefined();
expect(lastInFormGroup.attributes['valdr-message'].value).toBe('demoForm.fieldName');
});

it('should NOT add a the valdr-message after the input if no-valdr-message is set', function () {
// given
var element = compileTemplate(
Expand Down

0 comments on commit c7883ec

Please sign in to comment.