Skip to content

Commit

Permalink
New Features validation-submit & validation-reset
Browse files Browse the repository at this point in the history
  • Loading branch information
hueitan committed Jun 11, 2014
1 parent 1ce6869 commit b1d4a54
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 7 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ Writing your Code
};
// ...
</script>

<!-- In angular validation 1.2.0
More easy, more clean in your js code -->

<form name="Form">
<input type="text" name="number" ng-model="form.number" validator="number" valid-method="submit-only"/>
<button validation-submit="Form" ng-click="form.submit()"></button>
</form>

<script>
// Don't need include $validationProvider.validate() anymore
$scope.form = {
submit: function () {
// your ng-click success callback
}
};
</script>

<!-- Clean, right ? -->
```

**Setup a new Validation `setExpression()` `setDefaultMsg()` with `RegExp` or `Function` in config phase**
Expand Down Expand Up @@ -189,6 +208,8 @@ angular.module('yourApp', ['validation'])
<button ng-disabled="form.checkValid(Form)"></button>
<!-- Reset the specific Form -->
<button ng-click="form.reset(Form)"></button>
<!-- Clean Reset (angular validation 1.2.0) -->
<button validation-reset="Form"></button>
</form>
```

Expand All @@ -199,6 +220,11 @@ angular.module('yourApp', ['validation'])
$scope.form.reset = function (form) {
validationProvider.reset(form);
};
// ... angular validation 1.2.0 reset
$scope.form.reset = function () {
// Don't need include validationProvider.reset();
// Focus on your ng-click action
};
```

**Set the valid/invalid message style CSS**
Expand Down
10 changes: 6 additions & 4 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@
$scope.form = {
requiredCallback: 'required',
checkValid: $validationProvider.checkValid,
submit: function (form) {
$validationProvider.validate(form);
submit: function () {
// angular validation 1.2 can reduce this procedure, just focus on your action
// $validationProvider.validate(form);
},
reset: function (form) {
$validationProvider.reset(form);
reset: function () {
// angular validation 1.2 can reduce this procedure, just focus on your action
// $validationProvider.reset(form);
}
};

Expand Down
48 changes: 48 additions & 0 deletions dist/angular-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,5 +530,53 @@

}
};
}])

.directive('validationSubmit', ['$injector', function ($injector) {

var $validationProvider = $injector.get('$validation'),
$timeout = $injector.get('$timeout'),
$parse = $injector.get('$parse');

return {
priority: 1, // execute before ng-click (0)
terminal: true,
link: function postLink(scope, element, attrs) {
var form = attrs.validationSubmit;

$timeout(function () {
element.on('click', function (e) {
e.preventDefault();

$validationProvider.validate(scope[form])
.success(function () {
$parse(attrs.ngClick)(scope);
});
});
});

}
}
}])

.directive('validationReset', ['$injector', function ($injector) {

var $validationProvider = $injector.get('$validation'),
$timeout = $injector.get('$timeout');

return {
link: function postLink(scope, element, attrs) {
var form = attrs.validationReset;

$timeout(function () {
element.on('click', function (e) {
e.preventDefault();
$validationProvider.reset(scope[form]);
});
});

}
}
}]);

}).call(this);
2 changes: 1 addition & 1 deletion dist/angular-validation.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ <h1>Angular Validation.
</fieldset>

<ul class="button-group">
<li><button class="button" ng-click="form.submit(Form)">Submit</button></li>
<li><button class="button alert" ng-click="form.reset(Form)">Reset</button></li>
<li><button class="button" validation-submit="Form" ng-click="form.submit()">Submit</button></li>
<li><button class="button alert" validation-reset="Form" ng-click="form.reset()">Reset</button></li>
<li><button class="button secondary" ng-disabled="!form.checkValid(Form)">checkValid = {{ form.checkValid(Form) }}</button></li>
</ul>
<!--<pre>{{ form | json }}</pre>-->
Expand Down
48 changes: 48 additions & 0 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,53 @@

}
};
}])

.directive('validationSubmit', ['$injector', function ($injector) {

var $validationProvider = $injector.get('$validation'),
$timeout = $injector.get('$timeout'),
$parse = $injector.get('$parse');

return {
priority: 1, // execute before ng-click (0)
terminal: true,
link: function postLink(scope, element, attrs) {
var form = attrs.validationSubmit;

$timeout(function () {
element.on('click', function (e) {
e.preventDefault();

$validationProvider.validate(scope[form])
.success(function () {
$parse(attrs.ngClick)(scope);
});
});
});

}
}
}])

.directive('validationReset', ['$injector', function ($injector) {

var $validationProvider = $injector.get('$validation'),
$timeout = $injector.get('$timeout');

return {
link: function postLink(scope, element, attrs) {
var form = attrs.validationReset;

$timeout(function () {
element.on('click', function (e) {
e.preventDefault();
$validationProvider.reset(scope[form]);
});
});

}
}
}]);

}).call(this);

0 comments on commit b1d4a54

Please sign in to comment.