Comments (9)
from ngrx-forms.
this is 'stripped out' snippet from our app, where we are validating invoice.payments
elements (latter example from MrWolfZ answer):
export interface State {
form: FormGroupState<Invoice>;
dateTimeSettings: DateTimeSettings;
}
export interface Invoice extends Entity {
payments: Payment[];
}
export interface Payment extends Entity {
invoiceId: number;
description: string | null;
amount: number;
}
const FORM_NAME = 'INVOICE_DETAIL_FORM';
const initialForm = createForm(newInvoice(defaultDateTimeSettings), FORM_NAME);
export const initialState: State = {
form: initialForm,
dateTimeSettings: defaultDateTimeSettings,
};
const formReducerFn = (dateTimeSettings: DateTimeSettings) =>
createFormGroupReducerWithUpdate<Invoice>({
payments: updateArray<Payment>(
updateGroup<Payment>({
description: validate(customValidations.required),
amount: validate([
customValidations.required,
customValidations.double,
]),
}),
),
});
from ngrx-forms.
Pretty much what @nihique said, validate the items in the array but also that there's at least 1 which I managed to figure out myself.
from ngrx-forms.
I've tried to copy your example and get:
[ts]
Argument of type '{ name: ProjectFn<AbstractControlState<string>>; blah: ProjectFn<AbstractControlState<string | any...' is not assignable to parameter of type 'FormGroupState<TemplateRole>'.
Object literal may only specify known properties, and 'name' does not exist in type 'FormGroupState<TemplateRole>'.
with the model:
iterface Template {
name: string;
roles: TemplateRole[];
}
interface TemplateRole {
name: string;
}
with code:
createFormGroupReducerWithUpdate<Template>({
name: validate<string>(required),
roles: updateArray<TemplateRole>(
updateGroup<TemplateRole>({
name: validate<string>(required)
})
)
});
from ngrx-forms.
Yes, the typing for those update functions is a bit sub-optimal. This should do the trick:
createFormGroupReducerWithUpdate<Template>({
name: validate<string>(required),
roles: roles => updateArray<TemplateRole>(
cast(roles),
updateGroup<TemplateRole>({
name: validate<string>(required)
}),
)
});
With the next version (which I'll release as soon as TypeScript 2.8 is finally released) your version will work due to improved type inference for child controls. I will also clean up the update functions in general to be more consistent.
In regards to combining validation of array items with the whole array you would write the following:
createFormGroupReducerWithUpdate<Template>({
name: validate<string>(required),
roles: roles => {
roles = validate<TemplateRole[]>(minLength(1), roles);
return updateArray<TemplateRole>(
cast(roles),
updateGroup<TemplateRole>({
name: validate<string>(required)
}),
);
}
});
from ngrx-forms.
That works thanks.
But.
if you add another property to validate inside the updateArray, updateGroup or add another updateGroup you get the error again. I can't seem to find the right combination to validate multiple properties inside the array:(
from ngrx-forms.
Sorry for the late response. I hope you were able to find the right combination in the meantime. If not, to work around these typing issues (which I am in the process of fixing properly) you can always make the updateGroup
or updateArray
call explicit by providing an arrow function (Just as I did in my example for the updateArray
call. Basically, in my previous example you would adjust the reducer like this:
createFormGroupReducerWithUpdate<Template>({
name: validate<string>(required),
roles: roles => {
roles = validate<TemplateRole[]>(minLength(1), roles);
return updateArray<TemplateRole>(
cast(roles),
// by making the `role` state parameter explicit we ensure that the
// TypeScript compiler chooses the right overload
role => updateGroup<TemplateRole>(cast(role), {
name: validate<string>(required)
}),
);
}
});
from ngrx-forms.
Alright, so in the new and shiny TypeScript 2.8 world the following will type check:
interface Template {
name: string;
roles: TemplateRole[];
}
interface TemplateRole {
name: string;
}
createFormGroupReducerWithUpdate<Template>({
name: validate(required),
roles: compose(
validate<TemplateRole[]>(minLength(1)),
updateArray(
updateGroup<TemplateRole>({
name: validate(required),
}),
),
),
});
As far as I know Angular plans to drop TypeScript 2.8 support sometime in May at which point I will also release version 3.0.0 of ngrx-forms. I'll close this issue once that happens.
from ngrx-forms.
I have just released version 3.0.0. The code above should now properly type check.
from ngrx-forms.
Related Issues (20)
- Sure, just replace the players update with something like this:
- `NgrxFormControlDirective`: Update view when `ngrxValueConverter` changes HOT 1
- Pattern: Supply different "feature state" into the form reducer? HOT 1
- mat-radio checked not working? HOT 1
- Type inference is incorrect with readonly[]
- Question: Bind server-side validation to ngrx-forms forms
- [QUESTION] - Validation of a nested formState HOT 1
- Primitive unions are not supported
- Add compatibility with typescript 4.8 HOT 1
- New Angular Version 15 with breaking changes on Material to mat-list-options HOT 3
- No valid view adapter for Slider with Angular Material 15
- FormArrayState, the state doesn't update when SetValueActions are disptached
- Add compatibility with Typescript 5
- material 15+ (MCD based components) compatibility HOT 1
- Angular 16 support HOT 4
- Enable/Disable nested array properties HOT 3
- Programmatically Triggering Async Validation HOT 3
- Add sortArray function HOT 4
- Control validation order, run async after synchronous complete without error
- Question: Update parent form state based on child control states
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngrx-forms.