11import { computed , ref , Ref } from 'vue' ;
2- import { Flag , FormController , SubmissionHandler } from './types' ;
2+ import { Flag , FormController , SubmissionHandler , GenericValidateFunction } from './types' ;
33import { unwrap } from './utils/refs' ;
44
55const mergeStrategies : Record < Flag , 'every' | 'some' > = {
@@ -29,7 +29,11 @@ function computeFlags(fields: Ref<any[]>) {
2929 } , { } as Record < Flag , Ref < boolean > > ) ;
3030}
3131
32- export function useForm ( ) {
32+ interface FormOptions {
33+ validationSchema : Record < string , GenericValidateFunction > ;
34+ }
35+
36+ export function useForm ( opts ?: FormOptions ) {
3337 const fields : Ref < any [ ] > = ref ( [ ] ) ;
3438 const fieldsById : Record < string , any > = { } ;
3539 const values = computed ( ( ) => {
@@ -50,8 +54,14 @@ export function useForm() {
5054
5155 const controller : FormController = {
5256 register ( field ) {
57+ const vid = unwrap ( field . vid ) ;
58+ // Set the rules for that field from the schema.
59+ if ( opts ?. validationSchema [ vid ] ) {
60+ field . __setRules ( opts ?. validationSchema [ vid ] ) ;
61+ }
62+
5363 fields . value . push ( field ) ;
54- fieldsById [ unwrap ( field . vid ) ] = field ;
64+ fieldsById [ vid ] = field ;
5565 } ,
5666 fields : fieldsById ,
5767 values,
0 commit comments