Closed mxmtsk closed 7 years ago
I came up with this workaround, which does work but kinda breaks server side rendering with the following warning. So I'm still happy and open for suggestions or if you would consider adding support for this.
Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server: (client) ="text" id="email--32" name="email" valu (server) ="text" id="email--34" name="email" valu
Changed my LoginForm.js to
import validatorjs from 'validatorjs';
import MobxReactForm from 'mobx-react-form';
const plugins = { dvr: validatorjs };
const fields = [{
name: 'email',
label: 'Email',
rules: 'required|email',
}, {
name: 'password',
label: 'Password',
type: 'password',
rules: 'required',
}];
class LoginForm extends MobxReactForm {
constructor(successCallback) {
super({ fields }, { plugins });
this.callback = successCallback;
}
onSuccess = (form) => {
this.callback(form);
}
onError = (form) => {
// get all form errors
}
}
export default LoginForm;
And in my Login.js
class Login extends React.Component {
static propTypes = {
loginUser: React.PropTypes.func.isRequired,
}
constructor(props) {
super(props);
// initialize the form
this.form = new LoginForm(this.login);
}
login = (form) => {
// works
console.log(form.values());
}
[...]
Where tryLogin
is used?
Have you tried to Override the Validation Handlers into the component?
I think the problem about the SSR is because you have to initialize the Form instance outside the component.
thank you @foxhound87, Overriding validation handlers is exactly what I was looking for. I missed that in the documentation :)
Hey @mxmtsk, just note that you don't have to use onSuccess
or the other handlers in the mobx react form class.
Usually, I define my own submit handlers somewhere in one of my mobx store classes, and I basically use mobx-react-form for just defining the forms and exporting them.
@kitze now the built-in event handlers are able to also handle promises, if you use them you can track the new submitting
observable prop.
Is it possible to call a function on the component where the form is imported and displayed when I submit the form and validation passed?
I have some higher order components on my components (for example I'm using react-apollo for handling GraphQL requests) that bind certain methods to
this.props
that I don't have access to in the onSuccess method of the form, because that is not a real react component.I'd rather have the onSuccess method live in the Component where it get's used
LoginForm.js
Login.js