1.2.6 • Published 3 years ago

@eduenano27/react-form-validator v1.2.6

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

React Form Validation

Install

npm install @eduenano27/react-form-validator

Implementation

import { FormValidator } from "@eduenano27/react-form-validator";

export class MyFormComponent extends Component {
    constructor(props) {
        this.formValidator = new FormValidator({
            component: <component>,
            
            fields: {
                <fieldname>: <rules | rules + locale>
            },
            
            rules (optional): <custom rules>,
            message (optional): (error) => <error jsx>
        });
    }
    
    handleInputChange(event) {
        const fieldName  = event.target.name;
        const fieldValue = event.target.value;
        
        this.setState({
            [fieldName]: fieldValue
        });
        
        this.formValidator.validateOnly(fieldName, fieldValue);
    }
    
    handleSubmitForm(event) {
        const validateForm = this.formValidator.canSubmit();
        
        if(!validateForm) {
            //oops
            return;
        }
    }
    
    render() {
        { this.formValidator.hasError(<fieldname>) } <- has error (bool)
        { this.formValidator.getError(<fieldname>) } <- show error (jsx)
    }
}

Methods

  • formValidator.validateOnly(fieldName, fieldValue) Validates one field
  • formValidator.canSubmit(fieldsToUpdate <optional>) Checks if all fields are valid
  • formValidator.hasField(fieldName) Checks if field is included on form validation
  • formValidator.hasError(fieldName) Checks if field is not valid
  • formValidator.getError(fieldName). Returns JSX element with error
  • formValidator.reset(). Reset errors and fields

Rules

RuleSyntax
requiredrequired
emailemail
samesame:<field/string>
minmin:
maxmax:

Custom Rule Example

import { IFormFields } from "@eduenano27/react-form-validator/interfaces/fields/IFormFields";
import { IFormRuleReplacements } from "@eduenano27/react-form-validator/interfaces/rules/IFormRuleReplacements";
import { FormRule } from "@eduenano27/react-form-validator/rules/FormRule";

export class MyCustomRule extends FormRule {
    protected message: string = 'Field must have less than :max characters';

    public validate(value: string, params: string[], fields: IFormFields): boolean {
        var max = parseInt(params[0]);

        return value.length <= max;
    }

    public replacements(params: string[]): IFormRuleReplacements {
        return {
            'max': params[0]
        }
    }
}

Example

export class FormComponent extends Component {
    constructor(props) {
        this.formValidator = new FormValidator({
            component: this,
            
            fields: {
               username: {
                   rules: 'required|min:3|max:12',
                   
                   locale: {
                       required: 'Username is required!',
                       min: 'The username must have at least :min characters',
                       max: 'Username must be less than :max characters'
                   }
               },
               
               email: 'required|email',
               password: 'my_password_rule'
            },
            
            rules: {
                my_password_rule: new MyCustomRule
            },
            
            message: (error) => <div className="alert">¡Oops! { error }</div>
        });
    }

    handleInputChange(event) {
        const fieldName  = event.target.name;
        const fieldValue = event.target.value;
        
        this.setState({
            [fieldName]: fieldValue
        });
        
        this.formValidator.validateOnly(fieldName, fieldValue);
    }
    
    handleSubmitForm(event) {
        const validateForm = this.formValidator.canSubmit();
        
        if(!validateForm) {
            //oops
            return;
        }
    }
    
    render() {
        { this.formValidator.getError(<fieldname>) }
    }
}

Links

https://gitlab.com/eduenano27/react-form-validator/-/issues https://www.npmjs.com/package/@eduenano27/react-form-validator

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.0

3 years ago

1.1.8

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago