Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
86b7d868
Unverified
Commit
86b7d868
authored
4 years ago
by
Tom Robinson
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Improvements to Form error handling. Resolves #12291 (#12293)
parent
d15f5342
Loading
Loading
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
frontend/src/metabase/containers/Form.jsx
+49
-14
49 additions, 14 deletions
frontend/src/metabase/containers/Form.jsx
with
49 additions
and
14 deletions
frontend/src/metabase/containers/Form.jsx
+
49
−
14
View file @
86b7d868
...
...
@@ -8,6 +8,7 @@ import { createSelector } from "reselect";
import
{
reduxForm
,
getValues
,
initialize
,
change
}
from
"
redux-form
"
;
import
{
getIn
,
assocIn
}
from
"
icepick
"
;
import
_
from
"
underscore
"
;
import
{
t
}
from
"
ttag
"
;
import
CustomForm
from
"
metabase/components/form/CustomForm
"
;
import
StandardForm
from
"
metabase/components/form/StandardForm
"
;
...
...
@@ -82,6 +83,12 @@ type State = {
inlineFields
:
{
[
name
:
FormFieldName
]:
FormFieldDefinition
},
};
type
SubmitState
=
{
submitting
:
boolean
,
failed
:
boolean
,
result
:
any
,
};
let
FORM_ID
=
0
;
// use makeMapStateToProps so each component gets it's own unique formId
const
makeMapStateToProps
=
()
=>
{
...
...
@@ -95,19 +102,37 @@ const makeMapStateToProps = () => {
};
};
const
ReduxFormComponent
=
reduxForm
()(
props
=>
{
const
FormComponent
=
props
.
formComponent
||
(
props
.
children
?
CustomForm
:
StandardForm
);
return
<
FormComponent
{
...
props
}
/>;
});
const
ReduxFormComponent
=
reduxForm
()(
({
handleSubmit
,
submitState
,
...
props
})
=>
{
const
FormComponent
=
props
.
formComponent
||
(
props
.
children
?
CustomForm
:
StandardForm
);
return
(
<
FormComponent
{
...
props
}
handleSubmit
=
{
async
(...
args
)
=>
{
await
handleSubmit
(...
args
);
// normally handleSubmit swallows the result/error, but we want to make it available to things like ActionButton
if
(
submitState
.
failed
)
{
throw
submitState
.
result
;
}
else
{
return
submitState
.
result
;
}
}
}
/>
);
},
);
@
connect
(
makeMapStateToProps
)
export
default
class
Form
extends
React
.
Component
{
props
:
Props
;
state
:
State
;
_submitting
:
boolean
=
false
;
_submitFailed
:
boolean
=
false
;
_state
:
SubmitState
=
{
submitting
:
false
,
failed
:
false
,
result
:
undefined
,
};
_getFormDefinition
:
()
=>
FormDefinition
;
_getFormObject
:
()
=>
FormObject
;
...
...
@@ -232,8 +257,8 @@ export default class Form extends React.Component {
_validate
=
(
values
:
FormValues
,
props
:
any
)
=>
{
// HACK: clears failed state for global error
if
(
!
this
.
_submitting
&&
this
.
_s
ubmitF
ailed
)
{
this
.
_s
ubmitF
ailed
=
false
;
if
(
!
this
.
_
state
.
submitting
&&
this
.
_s
tate
.
f
ailed
)
{
this
.
_s
tate
.
f
ailed
=
false
;
props
.
stopSubmit
();
}
const
formObject
=
this
.
_getFormObject
();
...
...
@@ -243,27 +268,35 @@ export default class Form extends React.Component {
_onSubmit
=
async
(
values
:
FormValues
)
=>
{
const
formObject
=
this
.
_getFormObject
();
// HACK: clears failed state for global error
this
.
_submitting
=
true
;
this
.
_
state
.
submitting
=
true
;
try
{
const
normalized
=
formObject
.
normalize
(
values
);
return
await
this
.
props
.
onSubmit
(
normalized
);
return
(
this
.
_state
.
result
=
await
this
.
props
.
onSubmit
(
normalized
)
)
;
}
catch
(
error
)
{
console
.
error
(
"
Form submission error
"
,
error
);
this
.
_submitFailed
=
true
;
this
.
_state
.
failed
=
true
;
this
.
_state
.
result
=
error
;
// redux-form expects { "FIELD NAME": "FIELD ERROR STRING" } or {"_error": "GLOBAL ERROR STRING" }
if
(
error
&&
error
.
data
&&
error
.
data
.
errors
)
{
try
{
// HACK: blur the current element to ensure we show the error
document
.
activeElement
.
blur
();
}
catch
(
e
)
{}
throw
error
.
data
.
errors
;
// if there are errors for fields we don't know about then inject a generic top-level _error key
const
fieldNames
=
new
Set
(
this
.
_getFieldNames
());
const
errorNames
=
Object
.
keys
(
error
.
data
.
errors
);
const
hasUnknownFields
=
errorNames
.
some
(
name
=>
!
fieldNames
.
has
(
name
));
throw
{
_error
:
hasUnknownFields
?
t
`An error occurred`
:
null
,
...
error
.
data
.
errors
,
};
}
else
if
(
error
)
{
throw
{
_error
:
error
.
data
.
message
||
error
.
data
,
};
}
}
finally
{
setTimeout
(()
=>
(
this
.
_submitting
=
false
));
setTimeout
(()
=>
(
this
.
_
state
.
submitting
=
false
));
}
};
...
...
@@ -289,6 +322,8 @@ export default class Form extends React.Component {
validate
=
{
this
.
_validate
}
onSubmit
=
{
this
.
_onSubmit
}
onChangeField
=
{
this
.
_handleChangeField
}
// HACK: _state is a mutable object so we can pass by reference into the ReduxFormComponent
submitState
=
{
this
.
_state
}
/>
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment