Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support validation group #175

Merged
merged 7 commits into from
Dec 27, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ You can also add a custom validation message by using `message-id` attribute. It
<span id="message"></span>
```

### **Use a validation group** <br/>
You can also add a `validation-group` directive to group many elements into a group. The group will be considered as valid if and only if one of them is valid. Otherwise, the group will be marked as invalid. A valid/invalid message will be placed inside an element that contains an id attribute with the same name as provided to the directive `validation-group`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice description 👍


```html
<label>Validation group</label>
<!-- Group both of these elements inside the contact group -->
<input type="text" name="email" ng-model="email" validator="required" validation-group="contact">
<input type="number" name="telephone" ng-model="telephone" validator="number" validation-group="contact">
<!-- The message will be placed in side the span element -->
<span id="contact"></span>
```

<a name="no-validation-message"></a>
### **Don't show the Valid Message `no-validation-message="true"`**

Expand Down
33 changes: 24 additions & 9 deletions dist/angular-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
var validCallback = $parse('success');
var messageElem;

if (attrs.messageId) messageElem = angular.element(document.querySelector('#' + attrs.messageId));
if (attrs.messageId || attrs.validationGroup) messageElem = angular.element(document.querySelector('#' + (attrs.messageId || attrs.validationGroup)));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about define

var validationGroup = attrs.validationGroup

so that we use validationGroup instead of repeatly calling attrs.validationGroup within the function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great advice! Same for attrs.messageId. I'll fix both of them.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Same for other attributes :)

else messageElem = element.next();

if (element.attr('no-validation-message')) {
Expand Down Expand Up @@ -415,7 +415,7 @@
var invalidCallback = $parse('error');
var messageElem;

if (attrs.messageId) messageElem = angular.element(document.querySelector('#' + attrs.messageId));
if (attrs.messageId || attrs.validationGroup) messageElem = angular.element(document.querySelector('#' + (attrs.messageId || attrs.validationGroup)));
else messageElem = element.next();

if (element.attr('no-validation-message')) {
Expand All @@ -436,6 +436,18 @@
return false;
};

var checkValidationGroup = function(scope, element, attrs, ctrl) {
var validationGroupElems = document.querySelectorAll('*[validation-group=' + attrs.validationGroup + ']');
var validationGroupElem;

ctrl.$setValidity(ctrl.$name, false);

for (var i = 0, len = validationGroupElems.length; i < len; i++) {
validationGroupElem = angular.element(validationGroupElems[i]);
if (validationGroupElem.hasClass('ng-valid') && validationGroupElem[0] !== element[0]) return true;
}
return false;
};

/**
* collect elements for focus
Expand Down Expand Up @@ -489,7 +501,9 @@
return $q.all([$validationProvider.getExpression(validator)(value, scope, element, attrs, validatorParam)])
.then(function(data) {
if (data && data.length > 0 && data[0]) return valid.success();
else return valid.error();
else if (attrs.validationGroup) {
if (!checkValidationGroup(scope, element, attrs, ctrl)) valid.error();
} else return valid.error();
}, function() {
return valid.error();
});
Expand All @@ -499,11 +513,12 @@
else if (expression.constructor === RegExp) {
// Only apply the test if the value is neither undefined or null
if (value !== undefined && value !== null) return $validationProvider.getExpression(validator).test(value) ? valid.success() : valid.error();
else return valid.error();
else if (attrs.validationGroup) {
if (!checkValidationGroup(scope, element, attrs, ctrl)) valid.error();
} else return valid.error();
} else return valid.error();
};


/**
* generate unique guid
*/
Expand Down Expand Up @@ -554,7 +569,7 @@
/**
* Default Valid/Invalid Message
*/
if (!attrs.messageId) element.after('<span></span>');
if (!(attrs.messageId || attrs.validationGroup)) element.after('<span></span>');

/**
* Set custom initial validity
Expand All @@ -579,7 +594,7 @@
ctrl.$setPristine();
ctrl.$setValidity(ctrl.$name, undefined);
ctrl.$render();
if (attrs.messageId) angular.element(document.querySelector('#' + attrs.messageId)).html('');
if (attrs.messageId || attrs.validationGroup) angular.element(document.querySelector('#' + (attrs.messageId || attrs.validationGroup))).html('');
else element.next().html('');

if ($validationProvider.resetCallback) $validationProvider.resetCallback(element);
Expand Down Expand Up @@ -676,7 +691,7 @@
ctrl.$setViewValue(ctrl.$viewValue);
} else if (ctrl.$pristine) {
// Don't validate form when the input is clean(pristine)
if (attrs.messageId) angular.element(document.querySelector('#' + attrs.messageId)).html('');
if (attrs.messageId || attrs.validationGroup) angular.element(document.querySelector('#' + (attrs.messageId || attrs.validationGroup))).html('');
else element.next().html('');
return;
}
Expand All @@ -689,7 +704,7 @@
*/
attrs.$observe('noValidationMessage', function(value) {
var el;
if (attrs.messageId) el = angular.element(document.querySelector('#' + attrs.messageId));
if (attrs.messageId || attrs.validationGroup) el = angular.element(document.querySelector('#' + (attrs.messageId || attrs.validationGroup)));
else el = element.next();
if (value === 'true' || value === true) el.css('display', 'none');
else if (value === 'false' || value === false) el.css('display', 'block');
Expand Down
Loading