#311: fix validation in profile forms

This commit is contained in:
SleepWalker 2017-02-27 07:47:31 +02:00
parent 359ac19cc0
commit 1debe774ae
8 changed files with 49 additions and 9 deletions

View File

@ -323,12 +323,20 @@ export default class ChangeEmail extends Component {
onFormSubmit = () => {
const {activeStep} = this.state;
const promise = this.props.onSubmit(activeStep, this.props.stepForms[activeStep]);
const form = this.props.stepForms[activeStep];
const promise = this.props.onSubmit(activeStep, form);
if (!promise || !promise.then) {
throw new Error('Expecting promise from onSubmit');
}
promise.then(() => this.nextStep(), () => this.forceUpdate());
promise.then(() => this.nextStep(), (resp) => {
if (resp.errors) {
form.setErrors(resp.errors);
this.forceUpdate();
} else {
return Promise.reject(resp);
}
});
};
}

View File

@ -95,6 +95,15 @@ export default class ChangePassword extends Component {
}
onFormSubmit = () => {
this.props.onSubmit(this.props.form);
const {form} = this.props;
this.props.onSubmit(form)
.catch((resp) => {
if (resp.errors) {
form.setErrors(resp.errors);
} else {
return Promise.reject(resp);
}
});
};
}

View File

@ -80,6 +80,15 @@ export default class ChangeUsername extends Component {
};
onFormSubmit = () => {
this.props.onSubmit(this.props.form);
const {form} = this.props;
this.props.onSubmit(form)
.catch((resp) => {
if (resp.errors) {
form.setErrors(resp.errors);
} else {
return Promise.reject(resp);
}
});
};
}

View File

@ -13,5 +13,11 @@ export default function FormError({error}) {
}
FormError.propTypes = {
error: PropTypes.string
error: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
type: PropTypes.string.isRequired,
payload: PropTypes.object.isRequired
})
])
};

View File

@ -25,7 +25,13 @@ export default class Input extends FormInputComponent {
}),
PropTypes.string
]),
error: PropTypes.string,
error: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
type: PropTypes.string.isRequired,
payload: PropTypes.object.isRequired
})
]),
icon: PropTypes.string,
skin: PropTypes.oneOf(skins),
color: PropTypes.oneOf(colors),

View File

@ -26,7 +26,7 @@ class ChangePasswordPage extends Component {
onSubmit = () => {
const {form} = this;
this.context.onSubmit({
return this.context.onSubmit({
form,
sendData: () => accounts.changePassword(form.serialize())
}).then(() => {

View File

@ -45,10 +45,10 @@ class ChangeUsernamePage extends Component {
const {form} = this;
if (this.actualUsername === this.props.username) {
this.context.goToProfile();
return;
return Promise.resolve();
}
this.context.onSubmit({
return this.context.onSubmit({
form,
sendData: () => accounts.changeUsername(form.serialize())
}).then(() => {

View File

@ -86,6 +86,8 @@ export default connect(null, {
logger.warn('Unexpected profile editing error', {
resp
});
} else {
return Promise.reject(resp);
}
})
.finally(() => form.endLoading());