feat(form): change validate on load to true by default (#284)
This commit is contained in:
parent
b4901e87f9
commit
30de5161f2
4 changed files with 20 additions and 11 deletions
7
.changeset/sour-bobcats-peel.md
Normal file
7
.changeset/sour-bobcats-peel.md
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
---
|
||||||
|
"@astro-reactive/form": major
|
||||||
|
"@astro-reactive/demo": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
1. Change the default of `validateOnLoad` property to true, making server-side rendering validation the default behavior for the `Form` component.
|
||||||
|
1. Update the demo `index.astro` page by removing the `validateOnLoad` property/directive on the example which should still result to server-side rendered validation results.
|
|
@ -103,7 +103,6 @@ const submit: Submit = {
|
||||||
|
|
||||||
<Layout title={title} theme={theme}>
|
<Layout title={title} theme={theme}>
|
||||||
<Form
|
<Form
|
||||||
validateOnLoad
|
|
||||||
showValidationHints
|
showValidationHints
|
||||||
formGroups={form}
|
formGroups={form}
|
||||||
theme={theme}
|
theme={theme}
|
||||||
|
|
|
@ -56,7 +56,7 @@ const {
|
||||||
formGroups = [],
|
formGroups = [],
|
||||||
theme,
|
theme,
|
||||||
showValidationHints = false,
|
showValidationHints = false,
|
||||||
validateOnLoad = false,
|
validateOnLoad = true,
|
||||||
readOnly = false,
|
readOnly = false,
|
||||||
action = '',
|
action = '',
|
||||||
method = 'get',
|
method = 'get',
|
||||||
|
@ -72,10 +72,18 @@ const formId = Array.isArray(formGroups) ? uid() : formGroups?.id || null;
|
||||||
if (validateOnLoad) {
|
if (validateOnLoad) {
|
||||||
if (Array.isArray(formGroups)) {
|
if (Array.isArray(formGroups)) {
|
||||||
formGroups.forEach((group) =>
|
formGroups.forEach((group) =>
|
||||||
group.controls.forEach((control) => control.setValidateOnLoad(validateOnLoad))
|
group.controls.forEach((control) => {
|
||||||
|
if ('setValidateOnLoad' in control) {
|
||||||
|
control.setValidateOnLoad(validateOnLoad);
|
||||||
|
}
|
||||||
|
})
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
formGroups.controls.forEach((control) => control.setValidateOnLoad(validateOnLoad));
|
formGroups.controls.forEach((control) => {
|
||||||
|
if ('setValidateOnLoad' in control) {
|
||||||
|
control.setValidateOnLoad(validateOnLoad);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
---
|
---
|
||||||
|
|
|
@ -50,9 +50,8 @@ export class FormControl {
|
||||||
/**
|
/**
|
||||||
* Tracks the value and validation status of an individual form control.
|
* Tracks the value and validation status of an individual form control.
|
||||||
* @param config - interface of a `FormControl`'s configuration.
|
* @param config - interface of a `FormControl`'s configuration.
|
||||||
* @param validateOnLoad - determines if a control will be validated before rendering on the server. Defaults to `false`
|
|
||||||
*/
|
*/
|
||||||
constructor(private config: ControlConfig, validateOnLoad = false) {
|
constructor(private config: ControlConfig) {
|
||||||
const {
|
const {
|
||||||
name,
|
name,
|
||||||
type = 'text',
|
type = 'text',
|
||||||
|
@ -83,11 +82,6 @@ export class FormControl {
|
||||||
this._rows = rows;
|
this._rows = rows;
|
||||||
this._cols = cols;
|
this._cols = cols;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: implement independence
|
|
||||||
// form should try to import validator,
|
|
||||||
// but handle error when it's not installed
|
|
||||||
this.setValidateOnLoad(validateOnLoad);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get id() {
|
get id() {
|
||||||
|
@ -178,6 +172,7 @@ export class FormControl {
|
||||||
const valueStr: string = this._value?.toString() || '';
|
const valueStr: string = this._value?.toString() || '';
|
||||||
this._errors = this.validate(valueStr, this._validators);
|
this._errors = this.validate(valueStr, this._validators);
|
||||||
} else {
|
} else {
|
||||||
|
// give a console warning that the validator package needs to be installed
|
||||||
// if user did not install the validator, then errors should be empty
|
// if user did not install the validator, then errors should be empty
|
||||||
this._errors = [];
|
this._errors = [];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue