@@ -23,6 +23,8 @@ import {
23
23
SelectValue ,
24
24
} from "@/components/ui/select" ;
25
25
26
+ import DateFormField from "@/components/Form/FormFields/DateFormField" ;
27
+
26
28
import { GENDER_TYPES } from "@/common/constants" ;
27
29
28
30
import * as Notification from "@/Utils/Notifications" ;
@@ -74,8 +76,10 @@ const userFormSchema = z
74
76
)
75
77
. optional ( ) ,
76
78
phone_number_is_whatsapp : z . boolean ( ) . default ( true ) ,
77
- date_of_birth : z . string ( ) . min ( 1 , "Date of birth is required" ) ,
78
- gender : z . enum ( [ "male" , "female" , "other" ] ) ,
79
+ date_of_birth : z . date ( {
80
+ required_error : "Date of birth is required" ,
81
+ } ) ,
82
+ gender : z . enum ( [ "male" , "female" , "transgender" , "non_binary" ] ) ,
79
83
qualification : z . string ( ) . optional ( ) ,
80
84
doctor_experience_commenced_on : z . string ( ) . optional ( ) ,
81
85
doctor_medical_council_registration : z . string ( ) . optional ( ) ,
@@ -96,6 +100,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
96
100
const { t } = useTranslation ( ) ;
97
101
98
102
const form = useForm < UserFormValues > ( {
103
+ mode : "onChange" ,
99
104
resolver : zodResolver ( userFormSchema ) ,
100
105
defaultValues : {
101
106
user_type : "staff" ,
@@ -106,6 +111,10 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
106
111
} ,
107
112
} ) ;
108
113
114
+ const {
115
+ formState : { isDirty, errors, isValid } ,
116
+ } = form ;
117
+
109
118
const userType = form . watch ( "user_type" ) ;
110
119
const phoneNumber = form . watch ( "phone_number" ) ;
111
120
const isWhatsApp = form . watch ( "phone_number_is_whatsapp" ) ;
@@ -146,6 +155,8 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
146
155
} ) ;
147
156
}
148
157
} ;
158
+ console . log ( ! ! errors , errors ) ;
159
+ const required = < span className = "text-red-500" > *</ span > ;
149
160
150
161
return (
151
162
< Form { ...form } >
@@ -180,7 +191,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
180
191
name = "first_name"
181
192
render = { ( { field } ) => (
182
193
< FormItem >
183
- < FormLabel > First Name</ FormLabel >
194
+ < FormLabel > First Name{ required } </ FormLabel >
184
195
< FormControl >
185
196
< Input placeholder = "First name" { ...field } />
186
197
</ FormControl >
@@ -194,7 +205,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
194
205
name = "last_name"
195
206
render = { ( { field } ) => (
196
207
< FormItem >
197
- < FormLabel > Last Name</ FormLabel >
208
+ < FormLabel > Last Name{ required } </ FormLabel >
198
209
< FormControl >
199
210
< Input placeholder = "Last name" { ...field } />
200
211
</ FormControl >
@@ -209,7 +220,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
209
220
name = "username"
210
221
render = { ( { field } ) => (
211
222
< FormItem >
212
- < FormLabel > Username</ FormLabel >
223
+ < FormLabel > Username{ required } </ FormLabel >
213
224
< FormControl >
214
225
< Input placeholder = "Username" { ...field } />
215
226
</ FormControl >
@@ -224,7 +235,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
224
235
name = "password"
225
236
render = { ( { field } ) => (
226
237
< FormItem >
227
- < FormLabel > Password</ FormLabel >
238
+ < FormLabel > Password{ required } </ FormLabel >
228
239
< FormControl >
229
240
< Input type = "password" placeholder = "Password" { ...field } />
230
241
</ FormControl >
@@ -238,7 +249,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
238
249
name = "c_password"
239
250
render = { ( { field } ) => (
240
251
< FormItem >
241
- < FormLabel > Confirm Password</ FormLabel >
252
+ < FormLabel > Confirm Password{ required } </ FormLabel >
242
253
< FormControl >
243
254
< Input
244
255
type = "password"
@@ -257,7 +268,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
257
268
name = "email"
258
269
render = { ( { field } ) => (
259
270
< FormItem >
260
- < FormLabel > Email</ FormLabel >
271
+ < FormLabel > Email{ required } </ FormLabel >
261
272
< FormControl >
262
273
< Input type = "email" placeholder = "Email" { ...field } />
263
274
</ FormControl >
@@ -272,9 +283,14 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
272
283
name = "phone_number"
273
284
render = { ( { field } ) => (
274
285
< FormItem >
275
- < FormLabel > Phone Number</ FormLabel >
286
+ < FormLabel > Phone Number{ required } </ FormLabel >
276
287
< FormControl >
277
- < Input type = "tel" placeholder = "+91XXXXXXXXXX" { ...field } />
288
+ < Input
289
+ type = "tel"
290
+ placeholder = "+91XXXXXXXXXX"
291
+ maxLength = { 13 }
292
+ { ...field }
293
+ />
278
294
</ FormControl >
279
295
< FormMessage />
280
296
</ FormItem >
@@ -286,12 +302,13 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
286
302
name = "alt_phone_number"
287
303
render = { ( { field } ) => (
288
304
< FormItem >
289
- < FormLabel > Alternative Phone Number</ FormLabel >
305
+ < FormLabel > WhatsApp Number{ required } </ FormLabel >
290
306
< FormControl >
291
307
< Input
292
308
placeholder = "+91XXXXXXXXXX"
293
309
type = "tel"
294
310
{ ...field }
311
+ maxLength = { 13 }
295
312
disabled = { isWhatsApp }
296
313
/>
297
314
</ FormControl >
@@ -325,9 +342,14 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
325
342
name = "date_of_birth"
326
343
render = { ( { field } ) => (
327
344
< FormItem >
328
- < FormLabel > Date of Birth</ FormLabel >
345
+ < FormLabel > Date of Birth{ required } </ FormLabel >
329
346
< FormControl >
330
- < Input type = "date" { ...field } />
347
+ < DateFormField
348
+ name = "date_of_birth"
349
+ disableFuture
350
+ value = { field . value }
351
+ onChange = { ( date ) => field . onChange ( date . value ) }
352
+ />
331
353
</ FormControl >
332
354
< FormMessage />
333
355
</ FormItem >
@@ -437,7 +459,11 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
437
459
) }
438
460
/>
439
461
440
- < Button type = "submit" className = "w-full" >
462
+ < Button
463
+ type = "submit"
464
+ className = "w-full"
465
+ disabled = { ! isDirty || ! isValid }
466
+ >
441
467
Create User
442
468
</ Button >
443
469
</ form >
0 commit comments