10
10
>
11
11
<span class =" mb-2 block text-sm leading-4 text-gray-700" >
12
12
{{ field.label }}
13
+ <span
14
+ v-if =" field.required"
15
+ class =" place-self-center text-red-500"
16
+ >
17
+ *
18
+ </span >
13
19
</span >
14
20
<Input
15
21
v-if =" field.type === 'input'"
21
27
v-else
22
28
v-model =" state[field.value]"
23
29
:options =" customerResource.data"
24
- :value =" state[field.value]"
25
30
@update:model-value =" handleCustomerChange"
26
31
/>
27
32
<ErrorMessage :message =" error[field.error]" />
@@ -76,7 +81,7 @@ const state = ref({
76
81
firstName: " " ,
77
82
lastName: " " ,
78
83
phone: " " ,
79
- selectedCustomer: " " ,
84
+ selectedCustomer: null ,
80
85
});
81
86
82
87
const error = ref ({
@@ -92,6 +97,7 @@ interface FormField {
92
97
value: string ;
93
98
error: string ;
94
99
type: string ;
100
+ required: boolean ;
95
101
action? : () => void ;
96
102
}
97
103
@@ -101,34 +107,38 @@ const formFields: FormField[] = [
101
107
value: " emailID" ,
102
108
error: " emailValidationError" ,
103
109
type: " input" ,
110
+ required: true ,
104
111
action : () => validateEmailInput (state .value .emailID ),
105
112
},
106
113
{
107
114
label: " First Name" ,
108
115
value: " firstName" ,
109
116
error: " firstNameValidationError" ,
110
117
type: " input" ,
118
+ required: true ,
111
119
action : () => validateFirstName (state .value .firstName ),
112
120
},
113
121
{
114
122
label: " Last Name" ,
115
123
value: " lastName" ,
116
124
error: " lastNameValidationError" ,
117
125
type: " input" ,
126
+ required: false ,
118
127
},
119
128
{
120
129
label: " Phone" ,
121
130
value: " phone" ,
122
131
error: " phoneValidationError" ,
123
132
type: " input" ,
133
+ required: false ,
124
134
action : () => validatePhone (state .value .phone ),
125
135
},
126
136
{
127
137
label: " Customer" ,
128
138
value: " selectedCustomer" ,
129
139
error: " customerValidationError" ,
130
140
type: " autocomplete" ,
131
- action : () => validateCustomer ( state . value . selectedCustomer ) ,
141
+ required: false ,
132
142
},
133
143
];
134
144
@@ -165,7 +175,7 @@ const contactResource = createResource({
165
175
firstName: " " ,
166
176
lastName: " " ,
167
177
phone: " " ,
168
- selectedCustomer: " " ,
178
+ selectedCustomer: null ,
169
179
};
170
180
createToast ({
171
181
title: " Contact Created Successfully " ,
@@ -184,31 +194,34 @@ function createContact() {
184
194
first_name: state .value .firstName ,
185
195
last_name: state .value .lastName ,
186
196
email_ids: [{ email_id: state .value .emailID , is_primary: true }],
187
- links: [
188
- {
189
- link_doctype: " HD Customer" ,
190
- link_name: state .value .selectedCustomer ,
191
- },
192
- ],
197
+ links: [],
193
198
phone_nos: [],
194
199
};
195
200
if (state .value .phone ) {
196
201
doc .phone_nos = [{ phone: state .value .phone }];
197
202
}
203
+ if (state .value .selectedCustomer ) {
204
+ doc .links .push ({
205
+ link_doctype: " HD Customer" ,
206
+ link_name: state .value .selectedCustomer ,
207
+ });
208
+ }
198
209
199
210
contactResource .submit ({ doc });
200
211
}
201
212
202
- function handleCustomerChange(item : AutoCompleteItem ) {
203
- if (! item ) return ;
204
- state .value .selectedCustomer = item .value ;
213
+ function handleCustomerChange(item : AutoCompleteItem | null ) {
214
+ if (! item || item .label === " No label" ) {
215
+ state .value .selectedCustomer = null ;
216
+ } else {
217
+ state .value .selectedCustomer = item .value ;
218
+ }
205
219
}
206
220
207
221
function validateInputs() {
208
222
let error = validateEmailInput (state .value .emailID );
209
223
error += validateFirstName (state .value .firstName );
210
224
error += validatePhone (state .value .phone );
211
- error += validateCustomer (state .value .selectedCustomer );
212
225
return error ;
213
226
}
214
227
@@ -243,14 +256,6 @@ function validatePhone(value: string) {
243
256
return error .value .phoneValidationError ;
244
257
}
245
258
246
- function validateCustomer(value : string ) {
247
- error .value .customerValidationError = " " ;
248
- if (! value || value .trim () === " " ) {
249
- error .value .customerValidationError = " Customer should not be empty" ;
250
- }
251
- return error .value .customerValidationError ;
252
- }
253
-
254
259
function existingContactEmails(contacts ) {
255
260
return contacts .map ((contact ) => contact .email_id );
256
261
}
0 commit comments