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(binder): convert property paths in arrays to dot notation #3157

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
4 changes: 3 additions & 1 deletion packages/ts/lit-form/src/BinderRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ export class BinderRoot<M extends AbstractModel = AbstractModel> extends BinderN
const [property, value, message] = res ? res.splice(2) : [data.parameterName ?? '', undefined, data.message];
valueErrors.push({
message,
property,
// Convert property from bracket notation to dot notation
// Example: 'orders[0].description' becomes 'orders.0.description'
property: property.replace(/\[(\d+)\]/gu, '.$1'),
validator: new ServerValidator(message),
value,
validatorMessage: data.validatorMessage,
Expand Down
26 changes: 26 additions & 0 deletions packages/ts/lit-form/test/Validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,32 @@ describe('@vaadin/hilla-lit-form', () => {
}
});

it('should display server validation error for product description', async () => {
Lodin marked this conversation as resolved.
Show resolved Hide resolved
binder.for(binder.model.customer.fullName).value = 'foobar';
binder.for(binder.model.notes).value = 'whatever';
await fireEvent(orderView.add, 'click');
const productModel = [...binder.model.products][0].model;
binder.for(productModel.description).value = 'foobar';
binder.for(productModel.price).value = 10;
const requestUpdateSpy = sinon.spy(orderView, 'requestUpdate');
try {
await binder.submitTo(() => {
requestUpdateSpy.resetHistory();
throw new EndpointValidationError('Validation error in endpoint "MyEndpoint" method "saveMyBean"', [
new ValidationErrorData('Invalid description', 'products[0].description', 'Invalid description'),
]);
});
expect.fail();
} catch (error) {
sinon.assert.calledOnce(requestUpdateSpy);
await orderView.updateComplete;
const binderInArray = binder.for([...binder.model.products][0].model.description);
expect(binderInArray.invalid).to.be.true;
expect(binderInArray.ownErrors[0].message).to.equal('Invalid description');
expect(binderInArray.ownErrors[0].validatorMessage).to.equal('Invalid description');
}
});

it('should display submitting state during submission', async () => {
binder.for(binder.model.customer.fullName).value = 'Jane Doe';
binder.for(binder.model.notes).value = 'foo';
Expand Down
Loading