Skip to content

Commit

Permalink
Always track disabled fields
Browse files Browse the repository at this point in the history
This way, if they become enabled, we'll validate them. If they become disabled, we won't validate them.
  • Loading branch information
haacked committed Mar 18, 2024
1 parent 983f1c0 commit 33728fd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
7 changes: 6 additions & 1 deletion Pages/Demos/DisabledInputsNoWatch.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@
inputStatus.innerText = input.disabled ? 'disabled' : 'enabled';
toggleButton.innerText = input.disabled ? 'Enable' : 'Disable';
service.reset(input);
// If you dynamically disable a field, the next time validation occurs, the field will be ignored.
// If you dynamically enable a field, the next time validation occurs, the field will be validated.
// However, the current validation state will not be updated unless you call reset on the service
// or use the watch: true option in service.bootstrap.
//service.reset(input);
}
updateStatus('Value1');
Expand Down
6 changes: 1 addition & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,10 +1029,6 @@ export class ValidationService {
* @param input
*/
addInput(input: ValidatableElement) {
if (this.isDisabled(input)) {
return;
}

let uid = this.getElementUID(input);

let directives = this.parseDirectives(input.attributes);
Expand Down Expand Up @@ -1097,7 +1093,7 @@ export class ValidationService {

for (let i = 0; i < inputs.length; i++) {
let input = inputs[i];
if (remove || this.isDisabled(input)) {
if (remove) {
this.removeInput(input);
}
else {
Expand Down
12 changes: 10 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ export declare class ValidationService {
* @param inputUID
*/
private trackFormInput;
reset(input: HTMLElement): void;
private resetField;
private untrackFormInput;
/**
* Adds an input element to be managed and validated by the service.
Expand Down Expand Up @@ -306,6 +308,12 @@ export declare class ValidationService {
* @returns
*/
private isHidden;
/**
* Checks if the provided input is disabled
* @param input
* @returns
*/
private isDisabled;
/**
* Adds addClass and removes removeClass
* @param element Element to modify
Expand All @@ -320,7 +328,7 @@ export declare class ValidationService {
/**
* Load default validation providers and scans the entire document when ready.
* @param options.watch If set to true, a MutationObserver will be used to continuously watch for new elements that provide validation directives.
* @param options.addNoValidate If set to true (the default), a novalidate attribute will be added to the containing form in validate elemets.
* @param options.addNoValidate If set to true (the default), a novalidate attribute will be added to the containing form in validate elements.
*/
bootstrap(options?: Partial<ValidationServiceOptions>): void;
/**
Expand Down Expand Up @@ -358,7 +366,7 @@ export declare class ValidationService {
*/
ValidationSummaryCssClassName: string;
/**
* Override CSS class name for valid validation summary. Default: 'field-validation-valid'
* Override CSS class name for valid validation summary. Default: 'validation-summary-valid'
*/
ValidationSummaryValidCssClassName: string;
}

0 comments on commit 33728fd

Please sign in to comment.