GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing docs.nativebase.io — Form with Validation | NativeBase | Universal Components for React and React Native

Form with Validation | NativeBase | Universal Components for React and React Native

NativeBase
3.4.x
🎉 Prompt to React Native UI
→
Search
⌘
K
Hire us
NativeBase
Introduction
Getting Started
Installation
Setup NativeBase Provider
Migration
Upgrading to v3
Upgrading to 3.2.0 from 3.x
Upgrading to 3.4.0 from 3.3.x
Components Migration
ActionSheet
Badge
Button
Card
Checkbox
DatePicker
DeckSwiper
Drawer
FABs
FooterTab
Form
Header
Icon
Layout
Picker
Radio Button
Searchbar
Segment
Spinner
SwipeList
Tabs
Thumbnail
Toast
Typography
Resources
Contribution
Creating a Universal App with NativeBase and Solito
Core Concepts
Utility First
Utility Props Specificity
Design Tokens
Pseudo Props: Overview
Pseudo Props: Dos & Don'ts
Internal Pseudo Props
Responsive
Features
Utility Props
Color mode (Dark Mode)
NativeBase Factory
SafeAreaView Props
Strict Mode
Accessibility
Theme
Default Theme
Customizing Theme
Customizing Fonts
Customizing Components
Making components dark mode compatible
Breakpoints
Using Theme
TypeScript
Layout
AspectRatio
Box
Center
Container
Flex
HStack / Row
Stack
VStack / Column
ZStack
Forms
Button
Pressable
Checkbox
FormControl
IconButton
Input
Link
Radio
Select
Slider
Switch
TextArea
Data Display
Badge
Divider
Feedback
Alert
Progress
Skeleton
Spinner
Toast
Typography
Text
Heading
Overlay
AlertDialog
Menu
Modal
Popover
Tooltip
Disclosure
ActionSheet
Media and Icons
Avatar
Icon
Image
Transition
PresenceTransition
Slide
Stagger
Others
Fab
Hidden
ReactNative Components
ScrollView
View
KeyboardAvoidingView
StatusBar
FlatList
SectionList
Hooks
useDisclose
useBreakpointValue
useClipboard
useMediaQuery
useTheme
useToken
useColorMode
useColorModeValue
useContrastText
useAccessibleColors
Examples
Todo-List
Kitchen Sink
Recipes
Designs
AppBar
Card
Drawer Navigation
Tab View
Swipe List
Forms
Form with Validation
Login/Signup Forms
SearchBar
Layout
App Drawer
Footer
More
Changelog
Testing
FAQ's
More Props
If you are starting a new project, please try gluestack-ui for better performance and new design.
Form with Validation
Apps often require users to enter information into a text field. For example, you might require users to log in with an email address and password combination.
To make apps secure and easy to use, check whether the information the user has provided is valid. If the user has correctly filled out the form, process the information. If the user submits incorrect information, display a friendly error message letting them know what went wrong.
Example
In this example, learn how to add validation to a form that has a single text field using the following steps:
    Create an Input wrapped in FormControl.
    Add validation logic.
    Create a button to validate and submit the form.
Step 1
Create an Input wrapped in FormControl.
Playground
function BuildingAFormExample() {
const [formData, setData] = React.useState({});
return <VStack width="90%" mx="3" maxW="300px">
<FormControl isRequired>
<FormControl.Label _text={{
bold: true
}}>Name</FormControl.Label>
<Input placeholder="John" onChangeText={value => setData({ ...formData,
name: value
})} />
<FormControl.HelperText _text={{
fontSize: 'xs'
}}>
Name should contain atleast 3 character.
</FormControl.HelperText>
<FormControl.ErrorMessage _text={{
fontSize: 'xs'
}}>
Error Name
</FormControl.ErrorMessage>
</FormControl>
</VStack>;
}
function Example() {
return <Center flex={1}>
<BuildingAFormExample />
</Center>;
}
Step 2
Add validation logic.
Playground
function BuildingAFormExample() {
const [formData, setData] = React.useState({});
const [errors, setErrors] = React.useState({});
const validate = () => {
if (formData.name === undefined) {
setErrors({ ...errors,
name: 'Name is required'
});
return false;
} else if (formData.name.length < 3) {
setErrors({ ...errors,
name: 'Name is too short'
});
return false;
}
return true;
};
return <VStack width="90%" mx="3" maxW="300px">
<FormControl isRequired>
<FormControl.Label _text={{
bold: true
}}>Name</FormControl.Label>
<Input placeholder="John" onChangeText={value => setData({ ...formData,
name: value
})} />
<FormControl.HelperText _text={{
fontSize: 'xs'
}}>
Name should contain atleast 3 character.
</FormControl.HelperText>
<FormControl.ErrorMessage _text={{
fontSize: 'xs'
}}>
Error Name
</FormControl.ErrorMessage>
</FormControl>
</VStack>;
}
function Example() {
return <Center flex={1}>
<BuildingAFormExample />
</Center>;
}
Step 3
Create a button to validate and submit the form.
Playground
function BuildingAFormExample() {
const [formData, setData] = React.useState({});
const [errors, setErrors] = React.useState({});
const validate = () => {
if (formData.name === undefined) {
setErrors({ ...errors,
name: 'Name is required'
});
return false;
} else if (formData.name.length < 3) {
setErrors({ ...errors,
name: 'Name is too short'
});
return false;
}
return true;
};
const onSubmit = () => {
validate() ? console.log('Submitted') : console.log('Validation Failed');
};
return <VStack width="90%" mx="3" maxW="300px">
<FormControl isRequired isInvalid={'name' in errors}>
<FormControl.Label _text={{
bold: true
}}>Name</FormControl.Label>
<Input placeholder="John" onChangeText={value => setData({ ...formData,
name: value
})} />
{'name' in errors ? <FormControl.ErrorMessage>Error</FormControl.ErrorMessage> : <FormControl.HelperText>
Name should contain atleast 3 character.
</FormControl.HelperText>}
</FormControl>
<Button onPress={onSubmit} mt="5" colorScheme="cyan">
Submit
</Button>
</VStack>;
}
function Example() {
return <Center flex={1}>
<BuildingAFormExample />
</Center>;
}
Community Integration
NativeBase can be used with other popular Form libraries like
Formik
and
React Hook Forms
as well. For more details checkout Community Integration section of the docs.
Previous
Swipe List
Next
Login/Signup Forms
Quick Nav
Example
Step 1
Step 2
Step 3
Community Integration
MadeWithNativeBase
gluestack-ui
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.