Skip to content
Related Articles
Open in App
Not now

Related Articles

React Suite Form Validation Default Check

Improve Article
Save Article
  • Last Updated : 31 Aug, 2022
Improve Article
Save Article

React Suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application.

In this article, we’ll learn about React Suite Form Validation Default Check. A form is a set of components and models that process form data. In the default check, the form will automatically trigger the user data check after the submit event is triggered. In the Form validation check, the form will check If the user has entered the correct details like email, phone number, username, etc, and if it is incorrect, then the form will get an error and will not get submitted.

Syntax:

const { StringType, NumberType } = Schema.Types;
const validationChecker = Schema.Model({
    user: StringType().isRequired('required.'),
    email: StringType()
        .isEmail('Please enter a valid email address.')
        .isRequired('required.')
    ...
});
<Form>
    ...
</Form>

Creating React Application And Installing Module:

Step 1: Create a React application using the given command:

npm create-react-app projectname

Step 2: After creating your project, move to it using the given command:

cd projectname

Step 3: Now Install the rsuite node package using the given command:

npm install rsuite

Project Structure: Now your project structure should look like the following:

 

Example 1: Below example demonstrates the Form validation for the String Type data model.

Javascript




import "rsuite/dist/rsuite.min.css";
import { Form, Button, ButtonToolbar, Schema } from "rsuite";
  
const { StringType } = Schema.Types;
const validationChecker = Schema.Model({
    name: StringType().isRequired("Name is required."),
    email: StringType()
        .isEmail("Enter a valid email address.")
        .isRequired("Email is required."),
});
  
function TextField(props) {
    const { name, label, accepter, ...rest } = props;
    return (
        <Form.Group controlId={`${name}-3`}>
            <Form.ControlLabel>{label} </Form.ControlLabel>
            <Form.Control name={name} accepter={accepter}
                {...rest} />
        </Form.Group>
    );
}
  
export default function App() {
    return (
        <div>
            <div style={{ textAlign: "center" }}>
                <h2>GeeksforGeeks</h2>
                <h4 style={{ color: "green" }}>
                    React Suite Form Validation Default Check
                </h4>
            </div>
            <div style={{ padding: 20, textAlign: "center" }}>
                <div>
                    <Form model={validationChecker}>
                        <TextField name="name" 
                            label="Enter username" />
                        <TextField name="email" 
                            label="Enter email" />
                        <ButtonToolbar>
                            <Button appearance="primary"
                                type="submit" color="green">
                                Submit
                            </Button>
                        </ButtonToolbar>
                    </Form>
                </div>
            </div>
        </div>
    );
}


Output:

 

Example 2: Below example demonstrates the Form validation for the Integer Type data model.

Javascript




import "rsuite/dist/rsuite.min.css";
import {
    Form, Button, ButtonToolbar, Schema,
    Message, useToaster
} from "rsuite";
  
const { StringType, NumberType } = Schema.Types;
const validationChecker = Schema.Model({
    name: StringType().isRequired("Name is required."),
    email: StringType()
        .isEmail("Enter a valid email address.")
        .isRequired("Email is required."),
    phone: NumberType().isRequired('Phone is required.')
});
  
function TextField(props) {
    const { name, label, accepter, ...rest } = props;
    return (
        <Form.Group controlId={`${name}-3`}>
            <Form.ControlLabel>{label} </Form.ControlLabel>
            <Form.Control name={name} accepter={accepter}
                {...rest} />
        </Form.Group>
    );
}
export default function App() {
    const toaster = useToaster()
    const message = (
        <Message showIcon type={'success'}>
            Submitted.
        </Message>
    );
  
    return (
        <div>
            <div style={{ textAlign: "center" }}>
                <h2>GeeksforGeeks</h2>
                <h4 style={{ color: "green" }}>
                    React Suite Form Validation Default Check
                </h4>
            </div>
            <div style={{ padding: 20, textAlign: "center" }}>
                <div>
                    <Form model={validationChecker}>
                        <TextField name="name" 
                            label="Enter username" />
                        <TextField name="email" 
                            label="Enter email" />
                        <TextField name="phone" 
                            label="Enter phone" />
                        <ButtonToolbar>
                            <Button appearance="primary" 
                                type="submit"
                        onClick={() => toaster.push(message)} 
                                color="green">
                                Submit
                            </Button>
                        </ButtonToolbar>
                    </Form>
                </div>
            </div>
        </div>
    );
}


Output:

 

Reference: https://rsuitejs.com/components/form-validation/#default-check


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!