Skip to content
This repository has been archived by the owner on Aug 17, 2021. It is now read-only.

Commit

Permalink
Add required to opt out of validation
Browse files Browse the repository at this point in the history
Resolves #96 by adding and documenting the required attribute.
When require is `false` or is a scoped variable which is `false`,
the form validation will be removed.
This is opt out to prevent a breaking change and usually the validation is
preferred.
  • Loading branch information
TheSharpieOne committed Feb 6, 2016
1 parent 43d40b3 commit 4a8eb7e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ var app = angular.module('myApp', ['vcRecaptcha']);

Here, the `key` attribute is passed to the directive's scope, so you can use either a property in your scope or just a hardcoded string. Be careful to use your public key, not your private one.

Form Validation
---------------
**By default**, if placed in a [form](https://docs.angularjs.org/api/ng/directive/form) using [formControl](https://docs.angularjs.org/api/ng/type/form.FormController) the captcha will need to be checked for the form to be valid.
If the captcha is not checked (if the user has not checked the box or the check has expired) the form will be marked as invalid. The validation key is `recaptcha`.
You can **opt out** of this feature by setting the `required` attribute to `false` or a scoped variable
that will evaluate to `false`. Any other value, or omitting the attribute will opt in to this feature.

Response Validation
-------------------

Expand Down
25 changes: 16 additions & 9 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
theme: '=?',
size: '=?',
tabindex: '=?',
required: '=?',
onCreate: '&',
onSuccess: '&',
onExpire: '&'
Expand All @@ -31,6 +32,10 @@

scope.widgetId = null;

if(ctrl && angular.isDefined(attrs.required)){
scope.$watch('required', validate);
}

var removeCreationListener = scope.$watch('key', function (key) {
if (!key) {
return;
Expand All @@ -43,10 +48,9 @@
var callback = function (gRecaptchaResponse) {
// Safe $apply
$timeout(function () {
if(ctrl){
ctrl.$setValidity('recaptcha',true);
}
scope.response = gRecaptchaResponse;
validate();

// Notify about the response availability
scope.onSuccess({response: gRecaptchaResponse, widgetId: scope.widgetId});
});
Expand All @@ -62,9 +66,7 @@

}).then(function (widgetId) {
// The widget has been created
if(ctrl){
ctrl.$setValidity('recaptcha',false);
}
validate();
scope.widgetId = widgetId;
scope.onCreate({widgetId: widgetId});

Expand All @@ -86,14 +88,19 @@
}

function expired(){
if(ctrl){
ctrl.$setValidity('recaptcha',false);
}
scope.response = "";
validate();

// Notify about the response availability
scope.onExpire({widgetId: scope.widgetId});
}

function validate(){
if(ctrl){
ctrl.$setValidity('recaptcha', scope.required === false ? null : Boolean(scope.response));
}
}

function cleanup(){
// removes elements reCaptcha added.
angular.element($document[0].querySelectorAll('.pls-container')).parent().remove();
Expand Down

0 comments on commit 4a8eb7e

Please sign in to comment.