Skip to content

Commit d90831e

Browse files
authored
Merge pull request #122 from xsustek/feat/custom_highlight
Ability to specify custom highlight / unhighlight functions
2 parents c616741 + 66bfe61 commit d90831e

File tree

8 files changed

+72
-11
lines changed

8 files changed

+72
-11
lines changed

README.MD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,15 @@ v.ValidationMessageValidCssClassName = 'valid-feedback'; // change
288288
v.bootstrap();
289289
```
290290

291+
## Customizing highlight and unhighlight functions
292+
293+
```js
294+
var v = new aspnetValidation.ValidationService();
295+
v.highlight = function (input, errorClass, validClass) { ... };
296+
v.unhighlight = function (input, errorClass, validClass) { ... };
297+
v.bootstrap();
298+
```
299+
291300
## Logging
292301

293302
There is a rudimentary logging infrastructure in place if you want to get more insight into what the library is doing.

dist/aspnet-validation.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ var ValidationService = /** @class */ (function () {
11991199
this.swapClasses(spans[i], this.ValidationMessageCssClassName, this.ValidationMessageValidCssClassName);
12001200
}
12011201
}
1202-
this.swapClasses(input, this.ValidationInputCssClassName, this.ValidationInputValidCssClassName);
1202+
this.highlight(input, this.ValidationInputCssClassName, this.ValidationInputValidCssClassName);
12031203
if (input.form) {
12041204
// Adding an error to one input should also add it to others with the same name (i.e. for radio button and checkbox lists).
12051205
var inputs = input.form.querySelectorAll(validatableSelector("[name=\"".concat(input.name, "\"]")));
@@ -1223,7 +1223,7 @@ var ValidationService = /** @class */ (function () {
12231223
this.swapClasses(spans[i], this.ValidationMessageValidCssClassName, this.ValidationMessageCssClassName);
12241224
}
12251225
}
1226-
this.swapClasses(input, this.ValidationInputValidCssClassName, this.ValidationInputCssClassName);
1226+
this.unhighlight(input, this.ValidationInputCssClassName, this.ValidationInputValidCssClassName);
12271227
// Removing an error from one input should also remove it from others with the same name (i.e. for radio button and checkbox lists).
12281228
if (input.form) {
12291229
var inputs = input.form.querySelectorAll(validatableSelector("[name=\"".concat(input.name, "\"]")));
@@ -1439,6 +1439,24 @@ var ValidationService = /** @class */ (function () {
14391439
}
14401440
}
14411441
};
1442+
/**
1443+
* Highlights invalid element by adding errorClass CSS class and removing validClass CSS class
1444+
* @param input Element to modify
1445+
* @param errorClass Class to add
1446+
* @param validClass Class to remove
1447+
*/
1448+
ValidationService.prototype.highlight = function (input, errorClass, validClass) {
1449+
this.swapClasses(input, errorClass, validClass);
1450+
};
1451+
/**
1452+
* Unhighlight valid element by removing errorClass CSS class and adding validClass CSS class
1453+
* @param input Element to modify
1454+
* @param errorClass Class to remove
1455+
* @param validClass Class to add
1456+
*/
1457+
ValidationService.prototype.unhighlight = function (input, errorClass, validClass) {
1458+
this.swapClasses(input, validClass, errorClass);
1459+
};
14421460
return ValidationService;
14431461
}());
14441462

dist/aspnet-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.

dist/aspnet-validation.min.js.map

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

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aspnet-client-validation",
3-
"version": "0.11.0",
3+
"version": "0.11.1",
44
"description": "Enables ASP.NET MVC client-side validation, without jQuery!",
55
"main": "dist/aspnet-validation.js",
66
"style": "dist/aspnet-validation.css",

src/index.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ export class ValidationService {
12521252
}
12531253
}
12541254

1255-
this.swapClasses(input,
1255+
this.highlight(input,
12561256
this.ValidationInputCssClassName,
12571257
this.ValidationInputValidCssClassName);
12581258

@@ -1287,9 +1287,9 @@ export class ValidationService {
12871287
}
12881288
}
12891289

1290-
this.swapClasses(input,
1291-
this.ValidationInputValidCssClassName,
1292-
this.ValidationInputCssClassName);
1290+
this.unhighlight(input,
1291+
this.ValidationInputCssClassName,
1292+
this.ValidationInputValidCssClassName);
12931293

12941294
// Removing an error from one input should also remove it from others with the same name (i.e. for radio button and checkbox lists).
12951295
if (input.form) {
@@ -1513,6 +1513,26 @@ export class ValidationService {
15131513
}
15141514
}
15151515

1516+
/**
1517+
* Highlights invalid element by adding errorClass CSS class and removing validClass CSS class
1518+
* @param input Element to modify
1519+
* @param errorClass Class to add
1520+
* @param validClass Class to remove
1521+
*/
1522+
highlight(input : ValidatableElement, errorClass: string, validClass: string) {
1523+
this.swapClasses(input, errorClass, validClass);
1524+
}
1525+
1526+
/**
1527+
* Unhighlight valid element by removing errorClass CSS class and adding validClass CSS class
1528+
* @param input Element to modify
1529+
* @param errorClass Class to remove
1530+
* @param validClass Class to add
1531+
*/
1532+
unhighlight(input: ValidatableElement, errorClass: string, validClass: string) {
1533+
this.swapClasses(input, validClass, errorClass);
1534+
}
1535+
15161536
/**
15171537
* Override CSS class name for input validation error. Default: 'input-validation-error'
15181538
*/

types/index.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,20 @@ export declare class ValidationService {
353353
*/
354354
watch(root?: ParentNode): void;
355355
private observed;
356+
/**
357+
* Highlights invalid element by adding errorClass CSS class and removing validClass CSS class
358+
* @param input Element to modify
359+
* @param errorClass Class to add
360+
* @param validClass Class to remove
361+
*/
362+
highlight(input: ValidatableElement, errorClass: string, validClass: string): void;
363+
/**
364+
* Unhighlight valid element by removing errorClass CSS class and adding validClass CSS class
365+
* @param input Element to modify
366+
* @param errorClass Class to remove
367+
* @param validClass Class to add
368+
*/
369+
unhighlight(input: ValidatableElement, errorClass: string, validClass: string): void;
356370
/**
357371
* Override CSS class name for input validation error. Default: 'input-validation-error'
358372
*/

0 commit comments

Comments
 (0)