简单的登录表单,演示 FormState 的基础用法。
const form = useFormState({
fields: {
email: {
validator: Validators.compose(
Validators.required('邮箱不能为空'),
Validators.email('请输入有效的邮箱地址')
)
},
password: {
validator: Validators.compose(
Validators.required('密码不能为空'),
Validators.minLength(8, '密码至少需要 8 个字符')
)
}
}
});
<Form {form} onSubmit={handleSubmit}>
<FormField name="email" label="邮箱">
{#snippet children({ value, error, touched, onInput, onBlur })}
<input type="email" {value} oninput={e => onInput(e.target.value)} />
{/snippet}
</FormField>
</Form>{
"values": {
"email": "",
"password": "",
"rememberMe": false
},
"errors": {}
}