Skip to content

Commit ad0f55d

Browse files
committed
Differentiate submit submit-only method #4
Due to issue #4 Adding `submit` to validate after submit Adding `submit-only` to validate after only click submit
1 parent 0a96acb commit ad0f55d

File tree

6 files changed

+126
-10
lines changed

6 files changed

+126
-10
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Writing your Code
8787
<input type="text" name="requiredCallback" ng-model="form.requiredCallback" validator="required" invalid-callback='error("Must be Required");'/>
8888
```
8989

90-
**Select the validation method `watch` `blur` `submit`, default as `watch`** <br/>
90+
**Select the validation method `watch` `blur` `submit` `submit-only`, default as `watch`** <br/>
9191
`validationProvider.validate(form).success(callback).error(callback)` use callback to continue your submit
9292

9393
```html
@@ -103,6 +103,13 @@ Writing your Code
103103
<input type="text" name="number" ng-model="form.number" validator="number" valid-method="submit"/>
104104
<button ng-click="form.submit(Form)"></button>
105105
</form>
106+
107+
<label>Submit Only method</label>
108+
<form name="Form">
109+
<input type="text" name="number" ng-model="form.number" validator="number" valid-method="submit-only"/>
110+
<button ng-click="form.submit(Form)"></button>
111+
</form>
112+
106113
<script>
107114
// ... validate method, it will check `checkValid(Form)`
108115
$scope.form = {

demo/demo.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ angular.module('myApp', ['validation'])
4949
}
5050
};
5151

52+
$scope.form5 = {
53+
checkValid: $validationProvider.checkValid,
54+
submit: function (form) {
55+
$validationProvider.validate(form);
56+
},
57+
reset: function (form) {
58+
$validationProvider.reset(form);
59+
}
60+
};
61+
5262
// Callback method
5363
$scope.success = function (message) {
5464
alert('Success ' + message);

dist/angular-validation.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,17 @@
357357
invalidCallback: '&'
358358
},
359359
link: function (scope, element, attrs, ctrl) {
360+
361+
/**
362+
* watch
363+
* @type {watch}
364+
*
365+
* Use to collect scope.$watch method
366+
*
367+
* use watch() to destroy the $watch method
368+
*/
369+
var watch = function () {};
370+
360371
/**
361372
* validator
362373
* @type {*|Array}
@@ -365,7 +376,6 @@
365376
*/
366377
var validator = attrs.validator.split(',');
367378

368-
369379
/**
370380
* Valid/Invalid Message
371381
*/
@@ -380,6 +390,15 @@
380390
* Reset the validation for specific form
381391
*/
382392
scope.$on(ctrl.$name + 'reset', function () {
393+
394+
/**
395+
* clear scope.$watch here
396+
* when reset status
397+
* clear the $watch method to prevent
398+
* $watch again while reset the form
399+
*/
400+
watch();
401+
383402
ctrl.$setViewValue(null);
384403
ctrl.$setPristine();
385404
ctrl.$setValidity(ctrl.$name, false);
@@ -397,6 +416,25 @@
397416
*/
398417
scope.$on(ctrl.$name + 'submit', function () {
399418
var value = element[0].value;
419+
420+
if (attrs.validMethod === 'submit') {
421+
422+
watch(); // clear previous scope.$watch
423+
watch = scope.$watch('model', function (value) {
424+
425+
// scope.$watch will translate '' to undefined
426+
// undefined will pass the required submit /^.+/
427+
// cause some error in this validation
428+
if (value === undefined) {
429+
value = '';
430+
}
431+
432+
checkValidation(scope, element, attrs, ctrl, validation, value);
433+
});
434+
435+
return;
436+
}
437+
400438
checkValidation(scope, element, attrs, ctrl, validation, value);
401439
});
402440

@@ -415,9 +453,9 @@
415453
}
416454

417455
/**
418-
* Validate submit method
456+
* Validate submit & submit-only method
419457
*/
420-
if (attrs.validMethod === 'submit') {
458+
if (attrs.validMethod === 'submit' || attrs.validMethod === 'submit-only') {
421459
return;
422460
}
423461

dist/angular-validation.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
<div class="row">
1313
<div class="small-12 medium-12 large-12 large-centered columns">
1414
<h1>Angular Validation.
15+
1516
<small>
1617
<a href="#form1">Watch</a>.
1718
<a href="#form2">Blur</a>.
1819
<a href="#form3">Submit</a>.
19-
method.
20+
<a href="#form5">Submit-only</a>.
21+
<!--method.-->
2022
<a href="#form4">Additions</a>.
21-
validation
23+
<!--validation-->
2224
</small>
2325
</h1>
2426
</div>
@@ -176,6 +178,27 @@ <h1>Angular Validation.
176178
</div>
177179
</div>
178180

181+
<div class="row">
182+
<div class="small-12 medium-12 large-6 columns">
183+
<form name="Form5" id="form5">
184+
<fieldset>
185+
<legend>Form ($submit-only)</legend>
186+
<label>Required</label>
187+
<input type="text" name="emailSubmitOnly" ng-model="form5.required" validator="required" valid-method="submit-only"/>
188+
<label>Email</label>
189+
<input type="text" name="emailSubmitOnly" ng-model="form5.email" validator="email" valid-method="submit-only"/>
190+
</fieldset>
191+
192+
<ul class="button-group">
193+
<li><button class="button" ng-click="form5.submit(Form5)">Submit</button></li>
194+
<li><button class="button alert" ng-click="form5.reset(Form5)">Reset</button></li>
195+
<li><button class="button secondary" ng-disabled="!form5.checkValid(Form5)">checkValid = {{ form2.checkValid(Form2) }}</button></li>
196+
</ul>
197+
<!-- <pre>{{ form | json }}</pre> -->
198+
</form>
199+
</div>
200+
</div>
201+
179202
<a href="https://github.com/huei90/angular-validation"><img src="demo/iconmonstr-github-10-icon-128.png" id="github-link" alt="Fork me on Github"/></a>
180203
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
181204
<script src="dist/angular-validation.js"></script>

src/directive.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@
100100
invalidCallback: '&'
101101
},
102102
link: function (scope, element, attrs, ctrl) {
103+
104+
/**
105+
* watch
106+
* @type {watch}
107+
*
108+
* Use to collect scope.$watch method
109+
*
110+
* use watch() to destroy the $watch method
111+
*/
112+
var watch = function () {};
113+
103114
/**
104115
* validator
105116
* @type {*|Array}
@@ -108,7 +119,6 @@
108119
*/
109120
var validator = attrs.validator.split(',');
110121

111-
112122
/**
113123
* Valid/Invalid Message
114124
*/
@@ -123,6 +133,15 @@
123133
* Reset the validation for specific form
124134
*/
125135
scope.$on(ctrl.$name + 'reset', function () {
136+
137+
/**
138+
* clear scope.$watch here
139+
* when reset status
140+
* clear the $watch method to prevent
141+
* $watch again while reset the form
142+
*/
143+
watch();
144+
126145
ctrl.$setViewValue(null);
127146
ctrl.$setPristine();
128147
ctrl.$setValidity(ctrl.$name, false);
@@ -140,6 +159,25 @@
140159
*/
141160
scope.$on(ctrl.$name + 'submit', function () {
142161
var value = element[0].value;
162+
163+
if (attrs.validMethod === 'submit') {
164+
165+
watch(); // clear previous scope.$watch
166+
watch = scope.$watch('model', function (value) {
167+
168+
// scope.$watch will translate '' to undefined
169+
// undefined will pass the required submit /^.+/
170+
// cause some error in this validation
171+
if (value === undefined) {
172+
value = '';
173+
}
174+
175+
checkValidation(scope, element, attrs, ctrl, validation, value);
176+
});
177+
178+
return;
179+
}
180+
143181
checkValidation(scope, element, attrs, ctrl, validation, value);
144182
});
145183

@@ -158,9 +196,9 @@
158196
}
159197

160198
/**
161-
* Validate submit method
199+
* Validate submit & submit-only method
162200
*/
163-
if (attrs.validMethod === 'submit') {
201+
if (attrs.validMethod === 'submit' || attrs.validMethod === 'submit-only') {
164202
return;
165203
}
166204

0 commit comments

Comments
 (0)