tkrotoff / react-form-with-constraints

Simple form validation for React
MIT License
126 stars 20 forks source link

Validation errors not disappearing #52

Open Thorarin opened 4 years ago

Thorarin commented 4 years ago

I'm seeing some unexpected behavior when I trigger validation on a button press:

onSave = async (event: React.MouseEvent<HTMLButtonElement>) => {
        event.preventDefault();

        // I need to add this call, otherwise all the validation messages from
        // the previous submission attempt stay, even though the offending
        // input fields are fixed.
        this.form.current.reset();

        await this.form.current.validateForm();
        if (!this.form.current.isValid()) {
            console.log('Form invalid');
            return;
        }

        // Continue processing...
}

The examples don't have this reset() call, so I'm wondering what could cause this type of behavior? I can't think of anything "special" I did in my form that would cause this.

I'm using version 0.15.0.

tkrotoff commented 4 years ago

Difficult to help without a demo.

If you want to re-validate all fields, use validateFields().

validateForm() "validates only all non-dirty fields"

I guess you are using React classes. Follow this example: https://codepen.io/tkrotoff/pen/BRGdqL You should have: