@@ -57,6 +57,7 @@ class RecruitCRMJobApplyContainer extends React.Component {
57
57
[ key ] : value ,
58
58
} ,
59
59
} ) ) ;
60
+ this . validateForm ( key ) ;
60
61
}
61
62
62
63
onApplyClick ( ) {
@@ -70,7 +71,7 @@ class RecruitCRMJobApplyContainer extends React.Component {
70
71
} ) ;
71
72
}
72
73
73
- validateForm ( ) {
74
+ validateForm ( prop ) {
74
75
this . setState ( ( state ) => {
75
76
const { formData, formErrors } = state ;
76
77
// Form validation happens here
@@ -80,6 +81,9 @@ class RecruitCRMJobApplyContainer extends React.Component {
80
81
// check required text fields for value
81
82
// check min/max lengths
82
83
_ . each ( requiredTextFields , ( key ) => {
84
+ // validate only modified prop if set
85
+ // and do not touch the others
86
+ if ( prop && prop !== key ) return ;
83
87
if ( ! formData [ key ] || ! _ . trim ( formData [ key ] ) ) formErrors [ key ] = 'Required field' ;
84
88
else if ( formData [ key ] && _ . trim ( formData [ key ] ) . length < 2 ) formErrors [ key ] = 'Must be at least 2 characters' ;
85
89
else if ( formData [ key ] && _ . trim ( formData [ key ] ) . length > 2 ) {
@@ -101,36 +105,48 @@ class RecruitCRMJobApplyContainer extends React.Component {
101
105
} else delete formErrors [ key ] ;
102
106
} ) ;
103
107
// check for selected country
104
- if ( ! _ . find ( formData . country , { selected : true } ) ) formErrors . country = 'Please, select your country' ;
105
- else delete formErrors . country ;
108
+ if ( ! prop || prop === 'country' ) {
109
+ if ( ! _ . find ( formData . country , { selected : true } ) ) formErrors . country = 'Please, select your country' ;
110
+ else delete formErrors . country ;
111
+ }
106
112
// check payExpectation to be a number
107
- if ( formData . payExpectation && _ . trim ( formData . payExpectation ) ) {
108
- if ( ! _ . isInteger ( _ . toNumber ( formData . payExpectation ) ) ) formErrors . payExpectation = 'Must be integer value in $' ;
109
- else delete formErrors . payExpectation ;
110
- } else delete formErrors . payExpectation ;
113
+ if ( ! prop || prop === 'payExpectation' ) {
114
+ if ( formData . payExpectation && _ . trim ( formData . payExpectation ) ) {
115
+ if ( ! _ . isInteger ( _ . toNumber ( formData . payExpectation ) ) ) formErrors . payExpectation = 'Must be integer value in $' ;
116
+ else delete formErrors . payExpectation ;
117
+ } else delete formErrors . payExpectation ;
118
+ }
111
119
// check for valid email
112
- if ( formData . email && _ . trim ( formData . email ) ) {
113
- if ( ! ( isValidEmail ( formData . email ) ) ) formErrors . email = 'Invalid email' ;
114
- else delete formErrors . email ;
120
+ if ( ! prop || prop === 'email' ) {
121
+ if ( formData . email && _ . trim ( formData . email ) ) {
122
+ if ( ! ( isValidEmail ( formData . email ) ) ) formErrors . email = 'Invalid email' ;
123
+ else delete formErrors . email ;
124
+ }
115
125
}
116
126
// require atleast 1 skill
117
- if ( ! _ . find ( formData . skills , { selected : true } ) ) formErrors . skills = 'Please, add technical skills' ;
118
- else delete formErrors . skills ;
127
+ if ( ! prop || prop === 'skills' ) {
128
+ if ( ! _ . find ( formData . skills , { selected : true } ) ) formErrors . skills = 'Please, add technical skills' ;
129
+ else delete formErrors . skills ;
130
+ }
119
131
// have accepted terms
120
- if ( ! formData . agreedTerms ) formErrors . agreedTerms = 'Please, accept our terms' ;
121
- else delete formErrors . agreedTerms ;
132
+ if ( ! prop || prop === 'agreedTerms' ) {
133
+ if ( ! formData . agreedTerms ) formErrors . agreedTerms = 'Please, accept our terms' ;
134
+ else delete formErrors . agreedTerms ;
135
+ }
122
136
// has CV file ready for upload
123
- if ( ! formData . fileCV ) formErrors . fileCV = 'Please, pick your CV file for uploading' ;
124
- else {
125
- const sizeInMB = ( formData . fileCV . size / ( 1024 * 1024 ) ) . toFixed ( 2 ) ;
126
- if ( sizeInMB > 8 ) {
127
- formErrors . fileCV = 'Max file size is limited to 8 MB' ;
128
- delete formData . fileCV ;
129
- } else if ( _ . endsWith ( formData . fileCV . name , '.pdf' ) || _ . endsWith ( formData . fileCV . name , '.docx' ) ) {
130
- delete formErrors . fileCV ;
131
- } else {
132
- formErrors . fileCV = 'Only .pdf and .docx files are allowed' ;
133
- delete formErrors . fileCV ;
137
+ if ( ! prop || prop === 'fileCV' ) {
138
+ if ( ! formData . fileCV ) formErrors . fileCV = 'Please, pick your CV file for uploading' ;
139
+ else {
140
+ const sizeInMB = ( formData . fileCV . size / ( 1024 * 1024 ) ) . toFixed ( 2 ) ;
141
+ if ( sizeInMB > 8 ) {
142
+ formErrors . fileCV = 'Max file size is limited to 8 MB' ;
143
+ delete formData . fileCV ;
144
+ } else if ( _ . endsWith ( formData . fileCV . name , '.pdf' ) || _ . endsWith ( formData . fileCV . name , '.docx' ) ) {
145
+ delete formErrors . fileCV ;
146
+ } else {
147
+ formErrors . fileCV = 'Only .pdf and .docx files are allowed' ;
148
+ delete formErrors . fileCV ;
149
+ }
134
150
}
135
151
}
136
152
// updated state
0 commit comments