tcomb-form icon indicating copy to clipboard operation
tcomb-form copied to clipboard

Hold error message in state

Open Liooo opened this issue 7 years ago • 1 comments

Currently we need to set up state by ourselves in order to set error message at runtime (e.g. when showing an error message from server API), as discussed in this issue #19.

I thought it'd be much simpler to have the error message in the components' state and then change the error message directly.

After the this change, this code can be written as below

var RegisterForm = React.createClass({
...
    onClick: function(evt) {
        evt.preventDefault();
        var values = this.refs.registerForm.getValue();
        if (values) {
            var errors = sendDataToServer(values);
            if (errors.email) {
              var emailComp = this.refs.registerForm.getComponent('email')
              emailComp.setState({ hasError: true, error: errors.email })
            }
        }
    },
...
    render: function() {
        var Form = t.form.createForm(Model, {
            fields: {
                email: {
                    type: 'email',
                    label: 'Email',
                }
            }
        });

Liooo avatar Jun 18 '17 08:06 Liooo

Manipulating the child's state directly could be a bad practice, in which case we could expose an API like setError('error message') on the component

Liooo avatar Jun 18 '17 08:06 Liooo