Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(form): initialize value / defaultValue keys when rendering optional fields #2508

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions packages/ts/form/src/BinderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,13 @@ export class BinderNode<M extends AbstractModel = AbstractModel> extends EventTa
return undefined;
}

let { value } = this.parent;

if (value === undefined) {
this.parent.initializeValue(true);
({ value } = this.parent);
}
this.initializeValue();

const key = this.model[_key];

// The value of parent in unknown, so we need to cast it.
type ParentValue = { readonly [K in typeof key]: Value<M> };
return (value as ParentValue)[key];
return (this.parent.value as ParentValue)[key];
}

set value(value: Value<M> | undefined) {
Expand Down
2 changes: 2 additions & 0 deletions packages/ts/form/test/Binder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ describe('@hilla/form', () => {

const expectedEmptyOrder: Order = {
idString: '',
dateStart: undefined,
dateEnd: undefined,
customer: {
idString: '',
fullName: '',
Expand Down
17 changes: 17 additions & 0 deletions packages/ts/form/test/TestModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Size,
StringModel,
} from '../src/index.js';
import { Future } from '../Validators';

export interface IdEntity {
idString: string;
Expand Down Expand Up @@ -83,6 +84,8 @@ export class CustomerModel<T extends Customer = Customer> extends IdEntityModel<

export interface Order extends IdEntity {
customer: Customer;
dateStart?: string;
dateEnd?: string;
notes: string;
priority: number;
products: Product[];
Expand All @@ -99,6 +102,20 @@ export class OrderModel<T extends Order = Order> extends IdEntityModel<T> {
);
}

get dateStart(): StringModel {
return this[_getPropertyModel](
'dateStart',
(parent, key) => new StringModel(parent, key, true, { validators: [new Future()] }),
);
}

get dateEnd(): StringModel {
return this[_getPropertyModel](
'dateEnd',
(parent, key) => new StringModel(parent, key, true, { validators: [new Future()] }),
);
}

get notes(): StringModel {
return this[_getPropertyModel](
'notes',
Expand Down
33 changes: 33 additions & 0 deletions packages/ts/form/test/Validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
type Validator,
type ValueError,
} from '../src/index.js';
import { Future } from '../Validators';
import {
type Customer,
IdEntityModel,
Expand Down Expand Up @@ -176,6 +177,38 @@ describe('@hilla/form', () => {
]);
});

it('should run all validations after using binder.read()', async () => {
const valueFromEndpoint: Order = {
idString: '',
customer: {
idString: '',
fullName: 'Full Name',
nickName: 'Nick',
},
notes: 'ignore',
priority: 1,
products: [],
};
binder.read(valueFromEndpoint);

// Get nodes for optional fields to signal that they are bound
const dateStartNode = binder.for(binder.model.dateStart);
const dateEndNode = binder.for(binder.model.dateEnd);
expect(dateStartNode.value).to.be.undefined;
expect(dateEndNode.value).to.be.undefined;

// User sets values
dateStartNode.value = '2020-01-01';
dateEndNode.value = '2020-01-02';

const errors = await binder.validate();
expect(errors.length, 'number of errors').to.equal(2);
expect(errors[0].message).to.contain('future');
expect(errors[0].property).to.equal('dateStart');
expect(errors[0].message).to.contain('future');
expect(errors[1].property).to.equal('dateEnd');
});

describe('clearing', () => {
(['reset', 'clear'] as const).forEach((methodName) => {
it(`should reset validation on ${methodName}`, async () => {
Expand Down
Loading