11<script >
2- import { isNil , isBoolean , has , mapValues , noop } from ' lodash'
2+ import { isNil , isBoolean , isEmpty , has , mapValues , noop } from ' lodash'
33import { Form , Button } from ' element-ui'
44import FormItem from ' ./ConnectedFormItem'
55import Notification from ' ./Notification'
66import CONSTANTS from ' ./constants'
77import { validate , asyncValidate } from ' ./validators/validate'
8+ import isValid from ' ./utils/isValid'
89
910const BUTTONS_POSITION = {
1011 START : ' start' ,
@@ -52,7 +53,8 @@ export default {
5253 data () {
5354 return {
5455 state: {},
55- errors: {},
56+ syncErrors: {},
57+ asyncErrors: {},
5658 form: {
5759 submitting: false ,
5860 validating: false ,
@@ -73,7 +75,38 @@ export default {
7375
7476 computed: {
7577 isValid () {
76- return ! Object .values (this .errors ).some (error => ! isNil (error))
78+ return isValid ([this .syncErrors , this .asyncErrors ])
79+ },
80+
81+ isDisabled () {
82+ const { submitting , validating } = this .form
83+
84+ return submitting || validating
85+ },
86+
87+ isSubmitButtonDisabled () {
88+ return this .isDisabled || ! this .isValid
89+ },
90+
91+ submitButtonType () {
92+ return this .save ? ' danger' : ' primary'
93+ },
94+
95+ buttons () {
96+ return {
97+ reset: isBoolean (this .reset ) ? ' Reset' : this .reset ,
98+ save: isBoolean (this .save ) ? ' Save' : this .save ,
99+ submit: isBoolean (this .submit ) ? ' Submit' : this .submit ,
100+ }
101+ },
102+
103+ buttonsClassName () {
104+ return [
105+ ' buttons' , {
106+ buttons_center: this .buttonsPosition === BUTTONS_POSITION .CENTER ,
107+ buttons_end: this .buttonsPosition === BUTTONS_POSITION .END ,
108+ },
109+ ]
77110 },
78111 },
79112
@@ -93,17 +126,17 @@ export default {
93126
94127 const method = error ? vm .$set : vm .$delete
95128
96- method (vm .errors , name, error)
129+ method (vm .syncErrors , name, error)
97130 }
98131 }
99132
100133 const on = {
101- success : () => vm .$delete (vm .errors , name),
102- error : error => vm .$set (vm .errors , name, error),
134+ success : () => vm .$delete (vm .asyncErrors , name),
135+ error : error => vm .$set (vm .asyncErrors , name, error),
103136 }
104137
105138 const setAsyncError = () => {
106- if (! this .errors [name] && asyncValidators) {
139+ if (! this .syncErrors [name] && asyncValidators) {
107140 const offValidating = this .manageValidatingState ()
108141
109142 return asyncValidate (asyncValidators, this .state [name], name)
@@ -131,14 +164,18 @@ export default {
131164
132165 const cleanFormValue = () => {
133166 vm .$delete (this .state , name)
134- vm .$delete (this .errors , name)
167+ vm .$delete (this .syncErrors , name)
135168 }
136169
137170 return {
138171 cleanFormValue,
139172 setError,
140173 setAsyncError,
141- useState : () => [this .state [name], setValue, this .errors [name]],
174+ useState : () => [
175+ this .state [name],
176+ setValue,
177+ this .syncErrors [name] || this .asyncErrors [name],
178+ ],
142179 }
143180 },
144181
@@ -161,11 +198,22 @@ export default {
161198 nativeOnSubmit (event ) {
162199 event .preventDefault ()
163200
201+ if (this .validate ) {
202+ const syncErrors = this .validate (this .state )
203+
204+ if (! isEmpty (syncErrors)) {
205+ this .syncErrors = syncErrors
206+ return false
207+ }
208+
209+ this .syncErrors = {}
210+ }
211+
164212 // If Invalid
165213 // There is no SAVE BUTTON
166214 // There is a handleDisabled handler
167215 if (! this .isValid && ! this .save && this .handleDisabled ) {
168- return this .handleDisabled (this .errors )
216+ return this .handleDisabled ({ ... this .syncErrors , ... this . asyncErrors } )
169217 }
170218
171219 const messages = this .messages || {}
@@ -192,7 +240,8 @@ export default {
192240 const initialValue = this .initialValues [key]
193241
194242 vm .$set (this .state , key, initialValue || (Array .isArray (value) ? [] : ' ' ))
195- vm .$delete (this .errors , key)
243+ vm .$delete (this .syncErrors , key)
244+ vm .$delete (this .asyncErrors , key)
196245 })
197246
198247 if (this .handleReset ) {
@@ -202,46 +251,32 @@ export default {
202251
203252 reinitializeValues (updatedInitialValues ) {
204253 this .state = mapValues (this .state , (value , key ) => updatedInitialValues[key])
205- this .errors = {}
254+ this .syncErrors = {}
255+ this .asyncErrors = {}
206256 },
207257
208258 renderPlainButtons () {
209- const buttons = {
210- reset: isBoolean (this .reset ) ? ' Reset' : this .reset ,
211- save: isBoolean (this .save ) ? ' Save' : this .save ,
212- submit: isBoolean (this .submit ) ? ' Submit' : this .submit ,
213- }
214-
215- const buttonsClassName = [
216- ' buttons' , {
217- buttons_center: this .buttonsPosition === BUTTONS_POSITION .CENTER ,
218- buttons_end: this .buttonsPosition === BUTTONS_POSITION .END ,
219- },
220- ]
221-
222- const { submitting , validating } = this .form
223- const disabled = submitting || validating
224- const submitDisabled = disabled || (! this .isValid && ! this .handleDisabled )
225-
226259 return (
227- < div class = {buttonsClassName}>
260+ < div class = {this . buttonsClassName }>
228261 {this .reset && (
229- < Button nativeType= " reset" disabled= {disabled }>
230- {buttons .reset }
262+ < Button nativeType= " reset" disabled= {this . isDisabled }>
263+ {this . buttons .reset }
231264 < / Button>
232265 )}
233266 {this .save && (
234- < Button nativeType= " submit" type= " primary" disabled= {disabled }>
235- {buttons .save }
267+ < Button nativeType= " submit" type= " primary" disabled= {this . isDisabled }>
268+ {this . buttons .save }
236269 < / Button>
237270 )}
238271 {this .submit && (
239272 < Button
240- type= {this .save ? ' danger' : ' primary' }
273+ class = {[` el-button--${ this .submitButtonType } ` , {
274+ ' is-disabled' : this .isSubmitButtonDisabled ,
275+ }]}
276+ type= {this .submitButtonType }
241277 nativeType= {! this .save ? ' submit' : undefined }
242- disabled= {submitDisabled}
243278 on- click= {this .nativeOnSubmit }>
244- {buttons .submit }
279+ {this . buttons .submit }
245280 < / Button>
246281 )}
247282 < / div>
0 commit comments