Skip to content

Commit 273ae2c

Browse files
authored
fix(angular): apply touch, dirty and pristine form control classes (#24558)
Resolves #24483
1 parent 9a15753 commit 273ae2c

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

angular/src/directives/control-value-accessors/value-accessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class ValueAccessor implements ControlValueAccessor, AfterViewInit, OnDes
9696
if (formControl) {
9797
const methodsToPatch = ['markAsTouched', 'markAllAsTouched', 'markAsUntouched', 'markAsDirty', 'markAsPristine'];
9898
methodsToPatch.forEach((method) => {
99-
if (formControl.get(method)) {
99+
if (typeof formControl[method] !== 'undefined') {
100100
const oldFn = formControl[method].bind(formControl);
101101
formControl[method] = (...params: any[]) => {
102102
oldFn(...params);

angular/test/test-app/e2e/src/form.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ describe('Form', () => {
88
cy.get('#input-touched').click();
99
cy.get('#touched-input-test').should('have.class', 'ion-touched');
1010
});
11+
12+
describe('markAllAsTouched', () => {
13+
it('should apply .ion-touched to nearest ion-item', () => {
14+
cy.get('#mark-all-touched-button').click();
15+
cy.get('form ion-item').each(item => {
16+
cy.wrap(item).should('have.class', 'ion-touched');
17+
});
18+
});
19+
});
20+
1121
});
1222

1323
describe('change', () => {

angular/test/test-app/src/app/form/form.component.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
<ion-item>
1414
<ion-label>DateTime</ion-label>
15-
<ion-datetime formControlName="datetime" min="1994-03-14" max="2017-12-09" display-format="MM/DD/YYYY"></ion-datetime>
15+
<ion-datetime formControlName="datetime" min="1994-03-14" max="2017-12-09" display-format="MM/DD/YYYY">
16+
</ion-datetime>
1617
</ion-item>
1718

1819
<ion-item>
@@ -65,6 +66,7 @@
6566
<p>
6667
Form Submit: <span id="submit">{{submitted}}</span>
6768
</p>
69+
<ion-button id="mark-all-touched-button" (click)="markAllAsTouched()">Mark all as touched</ion-button>
6870
<ion-button id="submit-button" type="submit" [disabled]="!profileForm.valid">Submit</ion-button>
6971

7072
</form>

angular/test/test-app/src/app/form/form.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ export class FormComponent {
4646
});
4747
}
4848

49+
markAllAsTouched() {
50+
this.profileForm.markAllAsTouched();
51+
}
52+
4953
}

0 commit comments

Comments
 (0)