@@ -10,6 +10,7 @@ export interface FormValidationProps<Values> {
1010 initialValues : Values
1111 validate ?: Validations < Values > ,
1212 truthyValidationBeforeSubmit ?: boolean
13+ useInitialValidation ?: boolean // defaults to true
1314 onSubmit ?: ( values : Values ) => void
1415}
1516
@@ -41,15 +42,18 @@ class Validation<Values> implements IValidation<Values> {
4142 private readonly changeValue : ( key : string , value : any ) => void
4243 private readonly currentValues : Values
4344 private readonly currentValidations ?: Validations < Values >
45+ private readonly shouldValidate : boolean
4446
4547 constructor (
4648 changeValue : ( key : string , value : any ) => void ,
4749 values : Values ,
4850 validations : Validations < Values > ,
51+ shouldValidate : boolean = true
4952 ) {
5053 this . changeValue = changeValue
5154 this . currentValues = values
5255 this . currentValidations = validations
56+ this . shouldValidate = shouldValidate
5357 }
5458
5559 isValid ( ) : boolean {
@@ -78,12 +82,12 @@ class Validation<Values> implements IValidation<Values> {
7882 ? this . currentValidations [ key ] !
7983 : ( _value : Values [ Key ] ) => null
8084
81- const message = currentFc ( rawValue , this . currentValues )
85+ const message = this . shouldValidate ? currentFc ( rawValue , this . currentValues ) : null
8286
8387 return {
8488 // @ts -ignore – z.B. wenn dein Input `defaultValue` kennt
8589 defaultValue : currentValue ,
86- initialValue : currentValue || undefined ,
90+ initialValue : currentValue ?? undefined ,
8791 formValidation : {
8892 setValue : ( value : any ) => {
8993 this . changeValue ( currentName , value )
@@ -104,17 +108,20 @@ export const useForm = <
104108 Values extends Record < string , any > = Record < string , any >
105109> ( props : FormValidationProps < Values > ) : FormValidationReturn < Values > => {
106110
107- const { initialValues, validate = { } , truthyValidationBeforeSubmit = true , onSubmit} = props
111+ const { initialValues, validate = { } , truthyValidationBeforeSubmit = true , useInitialValidation = true , onSubmit} = props
108112
109113 const [ values , setValues ] = useState < Values > ( initialValues )
114+ const [ touched , setTouched ] = useState < boolean > ( false )
110115 const valuesRef = useRef < Values > ( initialValues )
111116
112117 useEffect ( ( ) => {
113118 setValues ( initialValues )
119+ setTouched ( false )
114120 valuesRef . current = initialValues
115121 } , [ initialValues ] )
116122
117123 const changeValue = useCallback ( ( key : keyof Values , value : any ) => {
124+ setTouched ( true )
118125 setValues ( prevState => {
119126 const nextState = {
120127 ...prevState ,
@@ -126,16 +133,17 @@ export const useForm = <
126133 } , [ ] )
127134
128135 const validation = useMemo (
129- ( ) => new Validation < Values > ( changeValue , values , validate ) ,
130- [ changeValue , values , validate ]
136+ ( ) => new Validation < Values > ( changeValue , values , validate , useInitialValidation || touched ) ,
137+ [ changeValue , values , validate , useInitialValidation , touched ]
131138 )
132139
133140 const validateFunction = useCallback ( ( ) => {
134141
135142 const currentValidation = new Validation < Values > (
136143 changeValue ,
137144 valuesRef . current ,
138- validate
145+ validate ,
146+ true
139147 )
140148
141149 if ( onSubmit && ( ! truthyValidationBeforeSubmit || currentValidation . isValid ( ) ) ) {
0 commit comments