Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid immediate validation of form elements built with formsy-react?

I'm using formsy-react to validate my React forms, but I don't want the validation to kick in before the user types. It seems to be working as long as the field is marked as "required", but when omitted, the error message shows immediately.

Am I doing it wrong?

var React = require('React');
var Formsy = require('formsy-react');

var MyOwnInput = React.createClass({
    mixins: [Formsy.Mixin],

    changeValue: function (event) {
        this.setValue(event.currentTarget.value);
    },
    render: function () {
        return (
            <div>
                <input type="text" onChange={this.changeValue} value={this.getValue()} placeholder={this.props.name}/>
                <span>{this.getErrorMessage()}</span>
            </div>
        );
    }
});

module.exports = React.createClass({
    render: function () {
        return (
            <Formsy.Form>
                <MyOwnInput name="email" validations="isEmail" validationError="This is not a valid email" required/>
                <MyOwnInput name="email2" validations="isEmail" validationError="This is not a valid email (no required attribute)"/>

                <button type="submit">Submit</button>
            </Formsy.Form>
        );
    }
});

enter image description here

like image 488
Remi Sture Avatar asked Jul 01 '15 08:07

Remi Sture


People also ask

How do you handle form validation in react?

Form validation in React allows an error message to be displayed if the user has not correctly filled out the form with the expected type of input. There are several ways to validate forms in React; however, this shot will focus on creating a validator function with validation rules.

How do you check if form field is touched and empty in react?

To check if an input is empty in React:Call the trim() method on the field's value. Access the length property on the value. If the field's value has a length of 0 , then it is empty, otherwise it isn't.

What are the several things we need to do in order to validate a form in react?

There are two ways of validating forms with React Final Form: record-level and field-level. Record level is pretty much the same as how it's done with Formik. And just like Formik, you can also easily use Yup for implementing validation rules.

How do I disable client side validation in my browser?

On your Google Chrome address bar, type “about:flags” (without the quote) and press Enter. 2. Scroll down the list until you see the option “Disable HTML5 interactive form validation”.


2 Answers

changeValue() will set the value, which in turn will validate it and the rest of the form. You could simply try to check for an empty string like this to prevent the validation at the beginning:

changeValue(event) {
    const newValue = event.currentTarget.value;
    if (newValue) this.setValue(newValue);
}

If it doesn't have the desired effect you could add a state that 'blocks' the validation until the first change.

getInitialState() {
    return {
        enableValidation: false
    }
}

changeValue(event) {
    const newValue = event;
    if (newValue && !this.state.enableValidation) this.setState({ enableValidation: true });
    if (newValue || this.state.enanbleValidation) this.setValue(newValue);
}
like image 61
marcel Avatar answered Oct 05 '22 02:10

marcel


I was able to overcome this issue, by using validationErrors in Form Level, instead of input level.

constructor(props) {
    super(props);
    this.state = {validationErrors: {}};
}


isNotValid(email) {
    // my validation code

}

validateForm(values) {
    if (isNotValid(values.email)) {
        this.setState({validationErrors: {email: 'my error messsage'}});
    } else {
        this.setState({validationErrors: {}});
    }
}


render() {
    return (
        <Formsy.Form
            onChange={this.validateForm}
            validationErrors={this.state.validationErrors}
        >
            <FormsyText
                floatingLabelText="some text"
                name="email"
                ref="email"
                value={this.state.email}
                onChange={(e) => this.handleChange('email', e)}
            />
        </Formsy.Form>
    );
}

The prop name="email" you pass to the component must be same as validationErrors: {email:} This way the input mapped to the error message.

It allows you to be in more control and manage the errors on every change.

like image 36
Uri Brodsky Avatar answered Oct 05 '22 02:10

Uri Brodsky