Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

Marjorie Zuehlke




Posted Answers



Answer


As developers, it is our job to ensure that when users interact with the forms we set up, the data they send across is in the form we expect.

In this article, we will learn how to handle form validation and track the state of forms without the aid of a form library. Next, we will see how the Formik library works. We’ll learn how it can be used incrementally with HTML input fields and custom validation rules. Then we will set up form validation using Yup and Formik’s custom components and understand how Yup works well with Formik in handling Form validation. We will implement these form validation methods to validate a simple sign up form I have set up.

Note: This article requires a basic understanding of React.

On its own, React is powerful enough for us to be able to set up custom validation for our forms. Let’s see how to do that. We’ll start by creating our form component with initial state values. The following sandbox holds the code for our form:

Form validation without the use of a library

With the useState hook, we set state variables for the formValues, formErrors and isSubmitting.

Here, we have 4 form handlers and a useEffect set up to handle the functionality of our form.

Here, we pass in the handleChange helper functions to the inputs’ onChange attribute. We link the value of the inputs to the formValues object, making them controlled inputs. From the React docs, controlled inputs are inputs whose values are controlled by React. An input-error style is applied if there are any errors related to that specific input field. An error message is conditionally displayed beneath each input if there are any errors related to that specific input field. Finally, we check if there are any errors in the errors object and if isSubmitting is true. If these conditions hold true, then we display a message notifying the user that they signed in successfully.

With this, we have a fully functional and validated form set up without the aid of a library. However, a form library like Formik with the aid of Yup can simplify the complexities of handling forms for us.

Right from the docs:

Formik is a flexible library. It allows you to decide when and how much you want to use it. We can control how much functionality of the Formik library we use. It can be used with HTML input fields and custom validation rules, or Yup and the custom components it provides. Formik makes form validation easy! When paired with Yup, they abstract all the complexities that surround handling forms in React.

Yup is a JavaScript object schema validator. While it has many powerful features, we’ll focus on how it helps us create custom validation rules so we don’t have to. This is a sample Yup object schema for a sign-up form. We’ll go into Yup and how it works in depth later in the article.

The following sandbox holds the code for this form set up:

The first thing we have to do is install Formik.

Then we can go ahead to import it in the file where we’ll make use of it.

Before creating the component, we need to create an initialValues and validate object which we’ll pass as props to the Formik component when we set it up. initialValues and validate are code snippets, not normal words.

The decision to do this outside the component is not a technical one, but rather for readability of our code.

initialValues: is an object that describes the initial values of the respective form fields. The name given to each key in the initialValues must correspond with the value of the name of the input field we want Formik to watch.

validate: this accepts a function that handles the form validation. The function accepts an object in the form of data values as an argument and validates each property in the object based on the rules defined. Each key in the values object must correspond with the name of the input field.

onSubmit: This handles what happens after the user submits. The onSubmit prop takes a callback function that will only run when there are no errors, meaning the user inputs are valid.

We pass in the initialValues object, and the submitForm and validate functions we defined earlier into Formik’s initialValues, onSubmit and validate props respectively.

Using the render props pattern, we have access to even more props the Formik API provides.

When the form is submitted, Formik checks if there are any errors in the errors object. If there are, it aborts the submission and displays the errors. To display the span using HTML inputs, we conditionally render and style the error message of each respective input field if the field has been touched and there are errors for that field.

Also, we can add a visual cue to the button. The button is conditionally styled and disable it if there are errors in the errors object using the isValid and the dirty props.

This sandbox holds the final code for this setup.

We install Yup, import the Field, Form, and the ErrorMessage components from Formik.

Formik makes form validation easy! When paired with Yup, they abstract all the complexities that surround handling forms in React. With that we can then go ahead to create the schema we’ll be using for the sign in form using Yup. Instead of creating custom validations for each possible input field, which can be tedious, depending on the number of fields there are, we can leave that to Yup to handle.

Yup works similarly to how we define propTypes in React. We created an object schema with Yup’s object function. We define the shape of the validation object schema and pass it into Yup’s shape() method. The required() method. This method takes a string as an argument, and this string will be the error message. that displays whenever a required field is left blank.

This schema has two properties:

We can chain validation is Yup as seen above. The properties of the schema object match the name of the input fields. The docs go into the different validation methods available in Yup.

While using HTML input fields get the job done, Formik’s custom components make things even easier for us, and reduce the amount of code we have to write! What are these custom components Formik provides us?

We pass the signInSchema into Formik using the validationSchema prop. The Formik team loves the Yup validation library so they created a specific prop for Yup called validationSchema which transforms errors into objects and matches against their values and touched functions.

Users do not know or care how you handle form validation. However, for you the developer, it should be as painless a process as possible, and I believe Formik stands out as a solid choice in that regard.

We have successfully looked at some of the options available to us when validating forms in React. We have seen how Formik can be used incrementally, and how it pairs well with Yup in handling form validation.


Answer is posted for the following question.

What is yup in formik?

Answer


In the most recent snapshot, this variant seems to be removed As a fan of lime green and a person whose base utilizes a lot of lime green ,


Answer is posted for the following question.

Why did mojang remove the green axolotl?

Answer


Explains how to change output to text files.

By default, the output is sent to the host. This is the console application. You can either change the output to a text file or the error output to the normal output stream.

The following methods can be used to change the output.

For more information about the output streams, see the information about them.

The following output streams are supported by PowerShell.

There is a progress stream, but it doesn't support redirecting.

The sequence number is represented by n in the PowerShell redirecting operators. If no stream is specified, the correct stream is the one that's right.

One element will succeed and one will fail in this example.

To send the resulting success stream to a file, use 2>&1 and then use the error stream as a separator.

This example shows how to transfer successful flow data to a file.

This example shows how you can combine operators.

This example shows how to send all output streams from a script to a file.

The example deleted all information flow data. The information flow cmdlets can be found in the Write-Host and Write-Information section.

Variables and action preference parameters can change what is written. This example script shows how the value of $ErrorActionPreference affects what is written.

When the script runs, it is prompted when the error action reference is set to Inquireen

We can see the following in the log file.

The current contents of the file are overwrited by the redirection operators that do not have data.

If the file is read only, hidden or system file, the redirecting fails. The operators do not write to a read-only file, but instead add content to a hidden system.

The Out-File cmdlet can be used to force the redirecting of content to a system, hidden, or read only file.

The redirection operators use the sameEncoding as when writing to files. The output may not be in the correct format if the file has a differentEncoding. To write to files with a differentEncoding, use the Out-File cmdlet.

When writing to a file using the Out-File or the redirecting operators, the output of the table to the file is formatted according to the width of the console in which it is executed. When logging the table output to the file using a command such as Get-ChildItem Env:Path > path.log, the file output is truncated to 80 characters.


Answer is posted for the following question.

How to save ls output to file?


Wait...