Skip to content

Commit c6c239c

Browse files
signalforms: update updateSalutationAndPronoun() (#45)
1 parent 8a212a8 commit c6c239c

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

blog/2025-10-signal-forms-part3/README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: 'Angular Signal Forms Part 3: Child Forms and Custom UI Controls'
33
author: Danny Koppenhagen and Ferdinand Malcher
44
mail: [email protected] # Gravatar
55
published: 2025-10-20
6-
lastModified: 2025-11-09
6+
lastModified: 2025-11-11
77
keywords:
88
- Angular
99
- Signals
@@ -99,7 +99,8 @@ The full form model still has to be defined in the parent component that manages
9999
However, parts of the `FieldTree` can be passed to child components via property binding.
100100
From the perspective of our `IdentityForm`, we receive the model from the parent component via an `input()`.
101101

102-
While we're at it, we also define a method `maybeUpdateSalutationAndPronoun()` that resets the salutation and pronoun fields when the user changes the gender from `diverse` to `male` or `female`.
102+
While we're at it, we also define a method `updateSalutationAndPronoun()` that resets the salutation and pronoun fields when the user changes the gender field.
103+
We set an empty value for the fields and call `reset()` which resets the `touched` and `dirty` state.
103104
Finally, we import the `Field` directive for binding the fields to our form elements in the template and our `FormError` component to be able to display validation errors.
104105

105106
```typescript
@@ -110,12 +111,11 @@ Finally, we import the `Field` directive for binding the fields to our form elem
110111
export class IdentityForm {
111112
readonly identity = model.required<FieldTree<GenderIdentity>>();
112113

113-
protected maybeUpdateSalutationAndPronoun() {
114-
const gender = this.identity().gender().value();
115-
if (gender !== 'diverse') {
116-
this.identity().salutation().value.set('');
117-
this.identity().pronoun().value.set('');
118-
}
114+
protected updateSalutationAndPronoun() {
115+
this.identity().salutation().value.set('');
116+
this.identity().salutation().reset();
117+
this.identity().pronoun().value.set('');
118+
this.identity().pronoun().reset();
119119
}
120120
}
121121
```
@@ -126,15 +126,15 @@ export class IdentityForm {
126126
In the template, things are straightforward and don't differ much from what we've seen so far:
127127
We use the `Field` directive to bind our fields to the form model.
128128
To conditionally show the fields for salutation and pronoun, we use the `hidden()` signal to determine whether the fields are marked as hidden.
129-
To trigger the reset logic, we bind the `change` event of the gender `<select>` to our method `maybeUpdateSalutationAndPronoun()`.
129+
To trigger the reset logic, we bind the `change` event of the gender `<select>` to our method `updateSalutationAndPronoun()`.
130130

131131
```html
132132
<label>
133133
Gender
134134
<select
135135
name="gender-identity"
136136
[field]="identity().gender"
137-
(change)="maybeUpdateSalutationAndPronoun()"
137+
(change)="updateSalutationAndPronoun()"
138138
>
139139
<option value="" selected>Please select</option>
140140
<option value="male">Male</option>
@@ -227,7 +227,7 @@ This is something that was relatively complicated with Angular' *Reactive Forms*
227227
Signal Forms provide a simple interface that allows us to create custom form components that integrate seamlessly with the Signal Forms ecosystem.
228228
Our goal is to create a custom component that can be used just like native HTML form elements with the `Field` directive.
229229

230-
### The `FormUiControl` interface
230+
### The `FormUiControl` interface
231231

232232
Signal Forms provide the `FormUiControl` interface that defines the contract for custom form components:
233233

0 commit comments

Comments
 (0)