-
-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] Return custom error message from onSubmit #161
Comments
Hey @cyclops24! Answer 0: Answer 1 (if you want to use class LoginForm extends Component {
constructor () {
super(...arguments);
this.state = {
loginError: null
};
this.onSubmit = this.onSubmit.bind(this);
this.onSubmitFailure = this.onSubmitFailure.bind(this);
this.onSubmitSuccess = this.onSubmitSuccess.bind(this);
}
onSubmit ({email, password}) {
return new Promise((resolve, reject) =>
Meteor.loginWithPassword(email, password, error =>
error ? reject(error) : resolve()
)
);
}
onSubmitFailure (error) {
// This error should be an Error instance with correct message, error
// recognised by your schema or a string.
this.setState({loginError: error});
}
onSubmitSuccess () {
browserHistory.push('/dashboard');
}
render () {
return (
<div>
<AutoForm
error={this.state.loginError}
onSubmit={this.onSubmit}
onSubmitFailure={this.onSubmitFailure}
onSubmitSuccess={this.onSubmitSuccess}
placeholder
schema={LoginSchema}
submitField={CustomSubmitField}
/>
<p className="account-form-text-after" id="forgot-password-link">
Forget password?
<Link to="/sign-up">
Click here!
</Link>
</p>
</div>
)
}
} Answer 2 (if you don't want to use // Change 1
<AutoForm
error={this.state.loginError}
onSubmit={this.onSubmit}
onSubmitFailure={this.onSubmitFailure}
onSubmitSuccess={this.onSubmitSuccess}
placeholder
schema={LoginSchema}
submitField={CustomSubmitField}
/>
// into
<AutoForm
error={this.state.loginError}
onSubmit={this.onSubmit}
placeholder
schema={LoginSchema}
submitField={CustomSubmitField}
/>
// Change 2
return new Promise((resolve, reject) =>
Meteor.loginWithPassword(email, password, error =>
error ? reject(error) : resolve()
)
);
// into
return new Promise((resolve, reject) =>
Meteor.loginWithPassword(email, password, error =>
error ? reject(error) : resolve()
).then(this.onSubmitSuccess, this.onSubmitFailure);
); And that's it. Side note 1: Side note 2: |
Thanks @radekmie , I'm going to start new open source example and reproduce it. |
Yes, I forgot about it - you should also reset this error after change... It's like this, because your form always have an error - that one from first login try. Which version are you using? Also, which browser? There is a silencer for these errors, but I can't recall, when it was introduced. |
@radekmie I reproduce this here: https://github.com/MeteorDemoApps/Uniforms-UserAccount-demo |
Yep, that was both problem in uniforms and in your form. I'll publish fix in a second and submit a PR in your demo. |
@radekmie Thanks man. It's fixed now. 😉 |
Hi @radekmie ,
I'm try to create a simple login form with Uniforms, SimpleSchema2 and Meteor.
This is my schema:
And this is my form component:
I'm using
this.setState({loginError: "error message"});
now but I think usingonSubmitSuccess
andonSubmitFailure
is simpler approach and also it's better because I think with this two props I can show error message on form validation error box like other error (Required and ...)I read #51 and https://github.com/vazco/uniforms/blob/master/API.md but can't understand how change my code for using promise.
Can you help me?
The text was updated successfully, but these errors were encountered: