NewOldMax / react-form-validator-core

Core validator component for react forms
MIT License
94 stars 44 forks source link

Trigger error after form submit and api return #12

Closed ianitsky closed 5 years ago

ianitsky commented 6 years ago

I have a return of the api, with an error for my form element. And I made a validation function. But, how can I trigger this after api returns my error?

ValidatorForm.addValidationRule('hasApiEmailError', (value) => {
    if (form_error && form_error.email && form_error.email.errors) {
        return false;
    }
    return true;
});

<TextValidator
    fullWidth
    label="Email"
    name="email"
    type="email"
    value={ user.email }
    validators={ ['required', 'isEmail', 'hasApiEmailError'] }
    errorMessages={ ['this field is required', 'email is not valid', this.getErrorMessage(form_error, 'email')] }
    onChange={ (event) => this.handleChange(event) }
/>
NewOldMax commented 6 years ago

Hi, you can call validation function manually for input or for form.

    <ValidatorForm ref={r => (this.form = r)} >
        <TextValidator ref={r => (this.input = r)} />
    </ValidatorForm>

runFormvalidation = () => {
    this.form.walk(this.form.childs);
}

runInputValidation = () => {
    this.input.validate(inputValue)
}
NewOldMax commented 5 years ago

Now you can use promises in your custom validations as well Check the 0.6.1 version