fix: update documentation and unit tests for validation
This commit is contained in:
parent
d129dd629d
commit
f421bd7326
2 changed files with 44 additions and 1 deletions
|
|
@ -346,7 +346,7 @@ export const emailValidator = () => html`
|
||||||
`;
|
`;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Override Default Messages
|
### Override default messages with custom text
|
||||||
|
|
||||||
To get default validation messages you need to import and call the `loadDefaultFeedbackMessages` function once in your application.
|
To get default validation messages you need to import and call the `loadDefaultFeedbackMessages` function once in your application.
|
||||||
|
|
||||||
|
|
@ -374,6 +374,22 @@ export const defaultMessages = () => html`
|
||||||
`;
|
`;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Override default messages with custom HTML
|
||||||
|
|
||||||
|
Similarly to overriding the default messages with a custom text it is possible to override the messages with a custom HTML.
|
||||||
|
|
||||||
|
```js preview-story
|
||||||
|
export const defaultMessagesWithCustomHtml = () => html`
|
||||||
|
<lion-input
|
||||||
|
.validators="${[
|
||||||
|
new EqualsLength(4, { getMessage: () => html`<div><b>Note</b> 4 chars please...</div>` }),
|
||||||
|
]}"
|
||||||
|
.modelValue="${'123'}"
|
||||||
|
label="Custom message for validator instance"
|
||||||
|
></lion-input>
|
||||||
|
`;
|
||||||
|
```
|
||||||
|
|
||||||
### Override fieldName
|
### Override fieldName
|
||||||
|
|
||||||
In the scenario that the default messages are correct, but you only want to change the `fieldName`, this can both be done for a single specific validator or for all at once.
|
In the scenario that the default messages are correct, but you only want to change the `fieldName`, this can both be done for a single specific validator or for all at once.
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import {
|
||||||
} from '@lion/ui/form-core-test-helpers.js';
|
} from '@lion/ui/form-core-test-helpers.js';
|
||||||
import sinon from 'sinon';
|
import sinon from 'sinon';
|
||||||
import {
|
import {
|
||||||
|
EqualsLength,
|
||||||
MaxLength,
|
MaxLength,
|
||||||
MinLength,
|
MinLength,
|
||||||
Required,
|
Required,
|
||||||
|
|
@ -779,6 +780,32 @@ export function runValidateMixinSuite(customConfig) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('getMessages with custom HTML', () => {
|
||||||
|
it('will render HTML for lion-validation-feedback', async () => {
|
||||||
|
const messageHtmlId = 'test123';
|
||||||
|
const messageHtml = `<div id="test123">test</div>`;
|
||||||
|
const el = /** @type {ValidateElement} */ (
|
||||||
|
await fixture(html`
|
||||||
|
<${tag}
|
||||||
|
.validators="${[
|
||||||
|
new EqualsLength(4, { getMessage: () => html`<div id="test123">test</div>` }),
|
||||||
|
]}" })]}"
|
||||||
|
.modelValue="${'123'}"
|
||||||
|
label="Custom message for validator instance"
|
||||||
|
></${tag}>
|
||||||
|
`)
|
||||||
|
);
|
||||||
|
expect(el.validationStates.error.EqualsLength).to.be.true;
|
||||||
|
expect(el.hasFeedbackFor).to.deep.equal(['error']);
|
||||||
|
const { _feedbackNode } = getFormControlMembers(el);
|
||||||
|
await el.updateComplete;
|
||||||
|
await el.updateComplete;
|
||||||
|
const messageHtmlNode = _feedbackNode.shadowRoot?.querySelector(`#${messageHtmlId}`);
|
||||||
|
expect(messageHtmlNode?.outerHTML).to.equal(messageHtml);
|
||||||
|
expect(messageHtmlNode?.tagName).to.equal('DIV');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('Required Validator integration', () => {
|
describe('Required Validator integration', () => {
|
||||||
it('will result in erroneous state when form control is empty', async () => {
|
it('will result in erroneous state when form control is empty', async () => {
|
||||||
const el = /** @type {ValidateElement} */ (
|
const el = /** @type {ValidateElement} */ (
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue