1.0.46 • Published 12 months ago

react-native-form-container v1.0.46

Weekly downloads
-
License
ISC
Repository
github
Last release
12 months ago

React Native Form Container

The purpose of this project is to provide a simple and flexible form validation library for React Native applications. With this library, developers can easily implement form validation logic and handle errors in their React Native forms.

šŸ“ Medium Article

Support the Project

If you would like to support the work I do on this project, you can buy me a coffee through Buy Me a Coffee. Every small contribution will be a big support for the development and sustainability of the project.

How Can You Help?

  • ā˜•ļø Buy Me a Coffee: You can support by buying a coffee. It's an easy and enjoyable way to contribute to the project.
  • ā­ļø Star Repository: You can help the project reach more people by starring the repo.
  • šŸ‘Øā€šŸ’» Contribute: You can directly contribute to the development of the project by working on it or providing feedback.

Thank You

Thank you for your support and interest! I need amazing community members like you to continue developing the project.

Installation

react-native-form-container

react-native-form-container is a simple and customizable form container component for React Native. This library simplifies form validation and manages form elements efficiently.

Features

  • Easy form management
  • Flexible and customizable
  • Simple validation for form elements

Installation

To add the react-native-form-container library to your project, use the following command:

npm install react-native-form-container

Usage

Below is an example showing how to use the react-native-form-container library.

Basic Usage

import React, { useState, useRef } from "react";
import { View, Text, SafeAreaView, Button } from "react-native";
import FormContainer, {   FormContainerRef,   FormInput, } from 'react-native-form-container';


export default function App() {
  const formContainerRef = useRef<FormContainerRef>(null);
  const [firstName, setFirstName] = useState("");
  const [lastName, setLastName] = useState("");

  return (
    <SafeAreaView>
      <FormContainer
        style={{ gap: 10, margin: 10 }}
        formContainerRef={formContainerRef}
      >
        <FormInput
          value={firstName}
          onChangeText={setFirstName}
          placeholder="First Name"
          id="firstName"
          required
        />
        <FormInput
          placeholder="Last Name"
          value={lastName}
          onChangeText={setLastName}
          id="lastName"
          required
        />
      </FormContainer>
      <Button
        onPress={() => {
          const errorMessages = {
            firstName: "Please enter your first name",
            lastName: "Please enter your last name",
          };
          const result = formContainerRef.current?.validate(errorMessages);
          console.log(result);
        }}
        title="Save"
      />
    </SafeAreaView>
  );
}
PropTypeDescription
stylePropTypes.object
formContainerRefPropTypes.oneOfType( PropTypes.func, PropTypes.shape({ current: PropTypes.instanceOf(Element) }) )

Form Validation

To trigger form validation, use the validate method of the form container reference. This method checks the validity of the fields and returns appropriate error messages for invalid fields.

const result = formContainerRef.current?.validate(errorMessages);
console.log(result);

Error Messages

You can pass error messages as an object to the validate method for each input component. For example:

const errorMessages = {
  firstName: "Please enter your first name",
  lastName: "Please enter your last name",
};

validate(errorMessages)

Method: validate(errorMessages: object) -> boolean

This method validates the validity of inputs in the form.

Parameters:

  • errorMessages: An object. Each key represents an input's identifier (ID), and its value is an error message.

Return Value:

  • true: If all inputs are valid.
  • false: If at least one input is invalid or the errorMessages parameter is not a valid object.

Example Usage:

const errorMessages = {
  firstName: "Please enter your first name",
  lastName: "Please enter your last name",
};

const result = formContainerRef.current?.validate(errorMessages);

Alternative Input Component

In this example, an alternative input component named AlternativeInput is defined. This component accepts props of type FormInputProps, which presumably includes properties required by a form input component.

Usage:

To use AlternativeInput, simply pass the necessary props expected by a form input component.

import React, { useState, useRef } from "react";
import { View, Text, SafeAreaView, Button, TextInput } from "react-native";
import FormContainer, {
  FormContainerRef,
  FormInputProps,
} from "react-native-form-container";
export default function App() {
  const formContainerRef = useRef < FormContainerRef > null;
  const [firstName, setFirstName] = useState("");
  const [lastName, setLastName] = useState("");
  const AlternativeInput = (props: FormInputProps) => {
    return (
      <View>
        <TextInput
          style={{
            borderWidth: 1,
            borderColor: "black",
            padding: 10,
            borderRadius: 5,
          }}
          {...props}
        />
        <View style={{ marginTop: 10 }}>
          {props.errorMessage && <Text>{props.errorMessage}</Text>}
        </View>
      </View>
    );
  };
  return (
    <SafeAreaView>
      <FormContainer
        style={{ gap: 10, margin: 10 }}
        formContainerRef={formContainerRef}
      >
        <AlternativeInput
          id="firstName"
          value={firstName}
          onChangeText={setFirstName}
          placeholder="First Name"
          required
        />
        <AlternativeInput
          placeholder="Last Name"
          value={lastName}
          onChangeText={setLastName}
          id="lastName"
          required
        />
      </FormContainer>

      <Button
        onPress={() => {
          const errorMessages = {
            firstName: "Please enter your first name",
            lastName: "Please enter your last name",
          };
          formContainerRef.current?.validate(errorMessages);
        }}
        title="Save"
      />
    </SafeAreaView>
  );
}

FormInputProps

The FormInput component accepts the following props:

PropTypeDescription
valuestringThe value of the input field.
onChangeTextfunctionCallback function to handle text changes.
placeholderstringPlaceholder text for the input field.
idstringUnique identifier for the input field. Required for validation.
requiredbooleanSpecifies whether the input field is required. If true, id must be provided.
patternstringRegex pattern for input validation. This works only if the validation prop is provided.
validationoneOf('email', 'password', 'text', 'phone', 'number')Specifies the type of input for predefined validation patterns.
passwordOptionsobjectAdditional options for password validation. See below for details.

passwordOptions

When the validation prop is set to password, the following passwordOptions can be specified:

OptionTypeDescription
minLengthnumberMinimum length of the password. Default value is 8.
specialbooleanChecks if the password contains special characters.
upperCasebooleanChecks if the password contains uppercase letters.
lowerCasebooleanChecks if the password contains lowercase letters.
numberbooleanChecks if the password contains numbers.

These validation rules are optional but can be specified to enforce stricter password criteria.

1.0.46

12 months ago

1.0.45

12 months ago

1.0.44

1 year ago

1.0.43

1 year ago

1.0.42

1 year ago

1.0.41

1 year ago

1.0.40

1 year ago

1.0.39

1 year ago

1.0.38

1 year ago

1.0.37

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.33

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago