@@ -4,7 +4,7 @@ import { Form, Button } from 'element-ui'
44import FormItem from ' ./ConnectedFormItem'
55import Notification from ' ./Notification'
66import CONSTANTS from ' ./constants'
7- import validate from ' ./validators/validate'
7+ import { validate , asyncValidate } from ' ./validators/validate'
88
99const BUTTONS_POSITION = {
1010 START : ' start' ,
@@ -43,6 +43,10 @@ export default {
4343 handleModelChange: Function ,
4444 handleDisabled: Function ,
4545 handleReset: Function ,
46+
47+ validate: Function ,
48+ asyncValidate: Function ,
49+ asyncBlurFields: Array ,
4650 },
4751
4852 data () {
@@ -51,6 +55,7 @@ export default {
5155 errors: {},
5256 form: {
5357 submitting: false ,
58+ validating: false ,
5459 },
5560 }
5661 },
@@ -81,35 +86,33 @@ export default {
8186 const value = ! isNil (formLevelInitialValue) ? formLevelInitialValue : fieldLevelInitialValue
8287
8388 const setError = nextValue => {
84- let validatorsPromise = Promise .resolve ()
85- let syncError = false
86-
87- const on = {
88- success : () => vm .$delete (vm .errors , name),
89- error : error => {
90- if (error) {
91- vm .$set (vm .errors , name, error)
92- }
93- },
94- }
95-
9689 if (validators) {
97- validatorsPromise = validate (validators, nextValue, name)
98- .then (on .success )
99- .catch (error => {
100- on .error (error)
101- syncError = true
102- })
90+ const offValidating = this .manageValidatingState ()
91+ const error = validate (validators, nextValue, name)
92+ offValidating ()
93+
94+ const method = error ? vm .$set : vm .$delete
95+
96+ method (vm .errors , name, error)
10397 }
98+ }
99+
100+ const on = {
101+ success : () => vm .$delete (vm .errors , name),
102+ error : error => vm .$set (vm .errors , name, error),
103+ }
104104
105- if (asyncValidators) {
106- validatorsPromise
107- // Prevent async validation when we have sync errors
108- . then (() => syncError && Promise . reject ())
109- . then (() => validate ( asyncValidators, nextValue , name) )
105+ const setAsyncError = () => {
106+ if ( ! this . errors [name] && asyncValidators) {
107+ const offValidating = this . manageValidatingState ()
108+
109+ return asyncValidate ( asyncValidators, this . state [name] , name)
110110 .then (on .success )
111111 .catch (on .error )
112+ .then (offValidating)
112113 }
114+
115+ return Promise .resolve ()
113116 }
114117
115118 const setValue = nextValue => {
@@ -134,10 +137,19 @@ export default {
134137 return {
135138 cleanFormValue,
136139 setError,
140+ setAsyncError,
137141 useState : () => [this .state [name], setValue, this .errors [name]],
138142 }
139143 },
140144
145+ manageValidatingState () {
146+ this .form .validating = true
147+
148+ return () => {
149+ this .form .validating = false
150+ }
151+ },
152+
141153 manageSubmittingState () {
142154 this .form .submitting = true
143155
@@ -158,12 +170,17 @@ export default {
158170
159171 const messages = this .messages || {}
160172 const off = this .manageSubmittingState ()
161- return Promise .resolve (this .handleSubmit ({ ... this .initialValues , ... this .state }))
162- .then (
163- () => Notification .success (messages .success ),
164- () => Notification .error (messages .error )
165- )
166- .then (off)
173+ const submitPromise = Promise .resolve (
174+ this .handleSubmit ({ ... this .initialValues , ... this .state })
175+ )
176+
177+ // Just subscribe to promise, do not catch errors
178+ submitPromise .then (
179+ () => Notification .success (messages .success ),
180+ () => Notification .error (messages .error )
181+ ).then (off)
182+
183+ return submitPromise
167184 },
168185
169186 nativeOnReset (event ) {
@@ -202,23 +219,27 @@ export default {
202219 },
203220 ]
204221
222+ const { submitting , validating } = this .form
223+ const disabled = submitting || validating
224+ const submitDisabled = disabled || (! this .isValid && ! this .handleDisabled )
225+
205226 return (
206227 < div class = {buttonsClassName}>
207228 {this .reset && (
208- < Button nativeType= " reset" disabled= {this . form . submitting }>
229+ < Button nativeType= " reset" disabled= {disabled }>
209230 {buttons .reset }
210231 < / Button>
211232 )}
212233 {this .save && (
213- < Button nativeType= " submit" type= " primary" disabled= {this . form . submitting }>
234+ < Button nativeType= " submit" type= " primary" disabled= {disabled }>
214235 {buttons .save }
215236 < / Button>
216237 )}
217238 {this .submit && (
218239 < Button
219240 type= {this .save ? ' danger' : ' primary' }
220241 nativeType= {! this .save ? ' submit' : undefined }
221- disabled= {( ! this . isValid && ! this . handleDisabled ) || this . form . submitting }
242+ disabled= {submitDisabled }
222243 on- click= {this .nativeOnSubmit }>
223244 {buttons .submit }
224245 < / Button>
0 commit comments