You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/FORM_ARRAYS.md
+6-3Lines changed: 6 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,13 +29,16 @@ Array states are completely independent of the DOM. They are updated by intercep
29
29
30
30
#### Dynamic Form Arrays
31
31
32
-
Sometimes you will have to render a variable number of fields in your form. Form arrays support adding and removing controls dynamically. This is done by setting the value of the form array via `setValue` which will automatically update the form array based on the value you provide.
32
+
Sometimes you will have to render a variable number of fields in your form. Form arrays support adding and removing controls dynamically. This can be done in two ways:
33
+
34
+
1) explicitly call the `addArrayControl` and `removeArrayControl` update functions (see the section section on [updating the state](UPDATING_THE_STATE.md) for more details on these functions)
35
+
2) set the value of the form array via `setValue` which will automatically update the form array based on the value you provide
33
36
34
37
Below you can find an example of how this would look. Assume that we have an action that provides a variable set of objects which each should be mapped to an array with two form controls.
Copy file name to clipboardExpand all lines: docs/FORM_GROUPS.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,14 +33,14 @@ Group states are usually completely independent of the DOM (with the exception o
33
33
34
34
Sometimes you will have to render a variable number of fields in your form. In such a case you can provide a form value interface that has an index signature and then add and remove controls dynamically. Instead of an index signature you can also use optional fields if the potential members of the form value are statically known. At runtime you can add and remove controls in two ways:
35
35
36
-
1) explicitly call the `addControl` and `removeControl` update functions (see the section below)
36
+
1) explicitly call the `addGroupControl` and `removeGroupControl` update functions (see the section section on [updating the state](UPDATING_THE_STATE.md) for more details on these functions)
37
37
2) set the value of the form group via `setValue` which will automatically update the form group based on the value you provide
38
38
39
39
Below you can find an example of how this would look. Assume that we have an action that provides a variable set of objects which each should be mapped to a group with two form controls.
Copy file name to clipboardExpand all lines: docs/UPDATING_THE_STATE.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
## Updating the State
2
2
3
-
All states are internally updated by ngrx-forms through dispatching actions. While this is of course also possible for you there exist a set of utility functions that can be used to update states. This is mainly useful to change the state as a result of a different action in your reducer. Note that ngrx-forms is coded in such a way that no state references will change if nothing inside the state changes. It is therefore perfectly safe to repeatedly call any of the functions below and the state will be updated exactly once or not at all if nothing changed. Each function can be imported from `ngrx-forms`. The following table explains each function:
3
+
All form states are internally updated by ngrx-forms through dispatching actions. While this is of course also possible for you there exist a set of update functions that can be used to update form states. This is mainly useful to change the state as a result of a different action in your reducer. Note that ngrx-forms is coded in such a way that no state references will change if nothing inside the state changes. It is therefore perfectly safe to repeatedly call any of the functions below and the state will be updated exactly once or not at all if nothing changed. Each function can be imported from `ngrx-forms`. The following table explains each function:
4
4
5
5
|Function|Description|
6
6
|-|-|
@@ -27,10 +27,10 @@ All states are internally updated by ngrx-forms through dispatching actions. Whi
27
27
These are the basic functions that perform simple updates on states. The functions below contain the real magic that allows easily updating deeply nested form states.
28
28
29
29
#### `updateArray`
30
-
This update function takes an update function and returns a function that takes an array state, applies the provided update function to each element and recomputes the state of the array afterwards. As with all the functions above this function does not change the reference of the array if the update function does not change any children. See the section below for an example of how this function can be used.
30
+
This update function takes an update function and returns a projection function that takes an array state, applies the provided update function to each element and recomputes the state of the array afterwards. As with all the functions above this function does not change the reference of the array if the update function does not change any children. See the section below for an example of how this function can be used.
31
31
32
32
#### `updateGroup`
33
-
This update function takes a partial object in the shape of the group's value where each key contains an update function for that child and returns a function that takes a group state, applies all the provided update functions recursively and recomputes the state of the group afterwards. As with all the functions above this function does not change the reference of the group if none of the child update functions change any children. The best example of how this can be used is simple validation:
33
+
This update function takes a partial object in the shape of the group's value where each key contains an update function for that child and returns a projection function that takes a group state, applies all the provided update functions recursively and recomputes the state of the group afterwards. As with all the functions above this function does not change the reference of the group if none of the child update functions change any children. The best example of how this can be used is simple validation:
Sometimes it is useful to apply an update function to all controls in a group or array recursively. This update function takes an update function and returns a function that takes any state and applies the provided update function to all its children, its children's children etc. and finally to the state itself. This means when the update function is called for a certain state all of its children will have already been updated. The provided update function takes 2 parameters, the state to update and its parent state. For the top-level state the state itself is passed as the second parameter.
196
+
Sometimes it is useful to apply an update function to all controls in a group or array recursively. This update function takes an update function and returns a projection function that takes any state and applies the provided update function to all its children, its children's children etc. and finally to the state itself. This means when the update function is called for a certain state all of its children will have already been updated. The provided update function takes 2 parameters, the state to update and its parent state. For the top-level state the state itself is passed as the second parameter.
197
197
198
198
Below you can find an example of how this function can be used. In this example we want to block all form inputs temporarily (e.g. while submitting the form). This can be done by disabling the form state at the root. However, when we unblock all inputs we want their enabled/disabled state to be reset to what it was before blocking the inputs. This could be done by simply storing a complete copy of the state (which might take a lot of space depending on the size of the form state). However, the example below uses a different method. We use the `setUserDefinedProperty` update function to store the enabled/disabled state before blocking the inputs and later restore them to the state they were in.
Copy file name to clipboardExpand all lines: docs/USER_GUIDE.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,21 +2,21 @@
2
2
3
3
The following sections will explain and showcase the different features of `ngrx-forms`. It is recommended to read these in the order they are listed below.
In addition to the synchronous validation via update functions explained above ngrx-forms supports asynchronous validation for form elements. However, since asynchronous validations are by nature not side-effect free they need to be handled differently.
47
47
48
-
ngrx-forms provides a set of 3 actions that can be used to perform asynchronous validation. These actions can be dispatched however you like, be that from a service or from within effects. The first of these actions is the `StartAsyncValidationAction` which takes the name of the validation to be performed. This name is added to the `pendingValidations` of the control state and the `isValidationPending` flag is set to true (if it was not already) for the control and all its parents. However, the validity of the control is not affected by this action. This means, if you e.g. want to disable the submit button of your form while the form is invalid or currently validating you need to check two properties, e.g. `[disabled]="formState.isInvalid || formState.isValidationPending"`. You can have as many asynchronous validations running at the same time as you like. The `isValidationPending` flag will be true as long as at least one validation has not yet completed.
48
+
ngrx-forms provides a set of three actions that can be used to perform asynchronous validation. These actions can be dispatched however you like, be that from a service or from within effects. The first of these actions is the `StartAsyncValidationAction` which takes the name of the validation to be performed. This name is added to the `pendingValidations` of the control state and the `isValidationPending` flag is set to true (if it was not already) for the control and all its parents. However, the validity of the control is not affected by this action. This means, if you e.g. want to disable the submit button of your form while the form is invalid or currently validating you need to check two properties, e.g. `[disabled]="formState.isInvalid || formState.isValidationPending"`. You can have as many asynchronous validations running at the same time as you like. The `isValidationPending` flag will be true as long as at least one validation has not yet completed.
49
49
50
50
The last two actions are used to complete the validation. The `SetAsyncErrorAction` takes the name of the validation and an arbitrary value and adds an error with the given name (prefixed with a `$`) and value to the state's errors. It also removes the validation from the control's `pendingValidations`. The `$` prefix marks all asynchronous errors which allows the synchronous validation via update functions to preserve these errors. That means you can safely combine synchronous validation and asynchronous validation. By adding the error the control will be marked as invalid if it was not already. The other action is the `ClearAsyncErrorAction` which takes only the name of the validation and removes the error if it was present. This action also removes the validation from the control's `pendingValidations`.
0 commit comments