{"id":4363,"date":"2023-05-04T12:50:21","date_gmt":"2023-05-04T05:50:21","guid":{"rendered":"https:\/\/www.bagi2info.com\/?p=4363"},"modified":"2023-10-03T13:02:30","modified_gmt":"2023-10-03T06:02:30","slug":"react-native-validasi-form-menggunakan-formik-dan-yup","status":"publish","type":"post","link":"https:\/\/www.bagi2info.com\/en\/react-native-form-validation-using-formik-and-yup\/","title":{"rendered":"React Native Form Validation Using Formik and Yup"},"content":{"rendered":"\r\n<p>I&#8217;ll make a basic form with name, phone number, email, password, and confirm password input fields to showcase easy form-building in react native. A state is a database window or screen that has numerous fields or spaces for data entry. Every field has a label, so every user who sees the form may see what it contains.<\/p>\r\n\r\n\r\n\r\n<p>A form is used in software development to acquire values from a user and save the information supplied by the user in the database via API calls. Working with the form&#8217;s input controls and react native form validation, on the other hand, is a bit difficult. This is where Formik and Yup come in; Formik allows you to build forms faster, while Yup handles validation.<\/p>\r\n\r\n\r\n\r\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\r\n\r\n\r\n\r\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.bagi2info.com\/en\/react-native-form-validation-using-formik-and-yup\/#What_Is_Formik\" >What Is Formik?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.bagi2info.com\/en\/react-native-form-validation-using-formik-and-yup\/#What_Is_Yup\" >What Is Yup?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.bagi2info.com\/en\/react-native-form-validation-using-formik-and-yup\/#Lets_Start_With_The_Implementation\" >Let\u2019s Start With The Implementation<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Is_Formik\"><\/span>What Is Formik?<span class=\"ez-toc-section-end\"><\/span><\/h2>\r\n\r\n\r\n\r\n<p>Formik is a simple but effective library that assists you with the three most challenging areas of react-native form creation. It also helps organizations by creating, testing, and refactoring forms.<\/p>\r\n\r\n\r\n\r\n<p>Form submission management<\/p>\r\n\r\n\r\n\r\n<p>Validation and warning messages<\/p>\r\n\r\n\r\n\r\n<p>Managing form state form values<\/p>\r\n\r\n\r\n\r\n<p><\/p>\r\n\r\n\r\n\r\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Is_Yup\"><\/span>What Is Yup?<span class=\"ez-toc-section-end\"><\/span><\/h2>\r\n\r\n\r\n\r\n<p>Yup is a JavaScript schema builder that supports value parsing and validation. Assign a schema, convert a value to match, validate the shape of an existing value, or both. Schema is a very effective tool for modeling complicated, interconnected validations or value transformations.<\/p>\r\n\r\n\r\n\r\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Lets_Start_With_The_Implementation\"><\/span>Let\u2019s Start With The Implementation<span class=\"ez-toc-section-end\"><\/span><\/h2>\r\n\r\n\r\n\r\n<p>We need to develop a state to keep the email value and a handle change method to handle text changes and update the email state when we add TextInputs for things like email addresses.<\/p>\r\n\r\n\r\n\r\n<p>This might be daunting, especially when working with a large number of inputs.<\/p>\r\n\r\n\r\n\r\n<p>This is where Formik comes in to help with all of the monotonous labor. So, let&#8217;s see how we can accomplish this with Formik by including an input form.<\/p>\r\n\r\n\r\n\r\n<pre title=\"App.js\" class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx line-numbers\">import React from 'react';\r\nimport {\r\n  SafeAreaView,\r\n  StyleSheet,\r\n  View,\r\n  Text,\r\n  TextInput,\r\n  Button,\r\n} from 'react-native';\r\nimport { Formik } from 'formik';\r\nimport * as yup from 'yup';\r\n\r\nconst loginValidationSchema = yup.object().shape({\r\n  email: yup\r\n    .string()\r\n    .email('Please enter valid email')\r\n    .required('Email Address is Required'),\r\n  password: yup\r\n    .string()\r\n    .min(8, ({ min }) => `Password must be at least ${min} characters`)\r\n    .required('Password is required'),\r\n});\r\n\r\nconst App = () => {\r\n  return (\r\n    &lt;>\r\n      &lt;SafeAreaView style={styles.container}>\r\n        &lt;Text style={styles.title}>\r\n          React Native Example{'\\n'}Login Screen using Formik And Yup{' '}\r\n        &lt;\/Text>\r\n        &lt;View style={styles.loginContainer}>\r\n          &lt;Formik\r\n            validateOnMount={true}\r\n            validationSchema={loginValidationSchema}\r\n            initialValues={{ email: '', password: '' }}\r\n            onSubmit={(values) => console.log(values)}>\r\n            {({\r\n              handleChange,\r\n              handleBlur,\r\n              handleSubmit,\r\n              values,\r\n              errors,\r\n              touched,\r\n              isValid,\r\n            }) => (\r\n              &lt;>\r\n                &lt;TextInput\r\n                  name=\"email\"\r\n                  placeholder=\"Email Address\"\r\n                  style={styles.textInput}\r\n                  onChangeText={handleChange('email')}\r\n                  onBlur={handleBlur('email')}\r\n                  value={values.email}\r\n                  keyboardType=\"email-address\"\r\n                \/>\r\n                {errors.email &amp;&amp; touched.email &amp;&amp; (\r\n                  &lt;Text style={styles.errorText}>{errors.email}&lt;\/Text>\r\n                )}\r\n\r\n                &lt;TextInput\r\n                  name=\"password\"\r\n                  placeholder=\"Password\"\r\n                  style={styles.textInput}\r\n                  onChangeText={handleChange('password')}\r\n                  onBlur={handleBlur('password')}\r\n                  value={values.password}\r\n                  secureTextEntry\r\n                \/>\r\n                {errors.password &amp;&amp; touched.password &amp;&amp; (\r\n                  &lt;Text style={styles.errorText}>{errors.password}&lt;\/Text>\r\n                )}\r\n\r\n                &lt;Button\r\n                  onPress={handleSubmit}\r\n                  title=\"LOGIN\"\r\n                  disabled={!isValid || values.email === ''}\r\n                \/>\r\n              &lt;\/>\r\n            )}\r\n          &lt;\/Formik>\r\n        &lt;\/View>\r\n      &lt;\/SafeAreaView>\r\n    &lt;\/>\r\n  );\r\n};\r\n\r\nconst styles = StyleSheet.create({\r\n  container: {\r\n    flex: 1,\r\n    justifyContent: 'center',\r\n    alignItems: 'center',\r\n    backgroundColor: '#ecf0f1',\r\n    padding: 8,\r\n  },\r\n  loginContainer: {\r\n    width: '80%',\r\n    alignItems: 'center',\r\n    padding: 10,\r\n    elevation: 10,\r\n  },\r\n  textInput: {\r\n    height: 40,\r\n    width: '100%',\r\n    margin: 10,\r\n    backgroundColor: 'white',\r\n    borderColor: 'gray',\r\n    borderWidth: StyleSheet.hairlineWidth,\r\n    borderRadius: 10,\r\n  },\r\n  title: {\r\n    textAlign: 'center',\r\n    fontSize: 18,\r\n    margin: 24,\r\n    fontWeight: 'bold',\r\n  },\r\n  errorText: {\r\n    fontSize: 10,\r\n    color: 'red',\r\n  },\r\n});\r\n\r\nexport default App;\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><\/p>\r\n\r\n\r\n\r\n<p>Link : <a href=\"https:\/\/snack.expo.dev\/@rudiahmad\/react-native---formik-example\" title=\"\">https:\/\/snack.expo.dev\/@rudiahmad\/react-native&#8212;formik-example<\/a><\/p>\r\n\r\n\r\n\r\n<p><\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ll make a basic form with name, phone number, email, password, and confirm password input fields to showcase easy form-building in react native. A state is a database window or screen that has numerous&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":4368,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,173],"tags":[150,145],"class_list":["post-4363","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-komputer","category-react-native","tag-react-native","tag-source-code"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts\/4363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/comments?post=4363"}],"version-history":[{"count":8,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts\/4363\/revisions"}],"predecessor-version":[{"id":4515,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts\/4363\/revisions\/4515"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/media\/4368"}],"wp:attachment":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/media?parent=4363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/categories?post=4363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/tags?post=4363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}