In the previous post, we have created our child modal components and a directive. Now it’s time to use them in our project.

Creating a new owner and using our child components and directives is our goal in this post.

If you want to see all the basic instructions and complete navigation for the .NET Core series, check out the following link: Introduction of the .NET Core series.

For the complete navigation and all the basic instructions of the Angular series, check out: Introduction of the Angular series.

For the previous part check outCreating Angular client side – @Input, @Output & Directives

The source code is available at GitHub .NET Core, Angular and MySQL. Part 13 – Source Code

This post is divided into several sections:

Preparation For the Create Owner Component

Let’s start by creating our component inside the owner folder. To do this, execute the Angular CLI command:

Create owner structure Angular Form Validation

Modify the owner module by adding this route:

When we click on the “create” link inside the owner-list.component.html we want the application to direct us to the creation page.

So, let’s modify the <a> tag inside the owner-list.component.html file:

About Form Validation and ReactiveFormsModule

Now we can start writing code to create our entity and to validate the form. There are two types of validation in the Angular: template driven validation and reactive form validation. In our project, we are going to use reactive form validation because it is easier to read an HTML file. Furthermore, it doesn’t make HTML file so much “dirty” with too many code lines and all validation is in the component which makes it easier for maintenance.

Just before modifying our component, we need to modify the owner.model.ts file. Let’s import the ReactiveFormsModule because this is a module which supports the reactive form validation:

Create a new interface:

Angular Form Validation Html Part

Modify the owner-create.component.html file:

Now let’s explain this code. In the form tag, we are creating the formGroup with a name ownerForm. This form group contains all the controls which we need to validate in our form. Moreover, with the (ngSubmit) we are calling a function when a user presses the submit button. As a parameter for that function, we are sending the ownerForm’s value which contains all the controls with the data we need for the validation.

There is a formControlName attribute inside every control. That attribute represents the control name which we are going to validate inside the ownerForm and it is a mandatory attribute. Furthermore, in the <em> tags we are displaying error messages if there are any. Errors will be written on the page only if the functions validateControl() and hasError() return true as a result.

The function validateControl() is going to check if the control is invalid and the hasError() function is going to check which validation rules we are validating against (required, max length…). Both validateControl and hasError functions are our custom functions which we are going to implement in the component file. There is also a submit button which is going to be disabled until form becomes valid and a cancel button which is going to redirect the user away from the creation form.

Furthermore, we import our child modal components inside this form to show success and error messages. As you might remember from the previous post, the success modal had the @Output decorator named redirectOnOk. This @Output decorator is emitting the EventEmmiter and in here we are subscribing to it with the event binding and assigning a function to it. The function redirectToOwnerList() is going to be executed as soon as the user clicks on the OK button from the success modal window.

Excellent, let’s continue on.

Angular Form Validation Component Part

Modify the owner-create.component.ts file:

Let’s explain this code. As soon as a component mounts we are initializing our FormGroup variable named ownerForm with all the FormControls. Pay attention that the keys in the ownerForm object are the same as the names in the formControlName attribute for all input fields in a .html file, which is mandatory. Moreover, they have the same name as the properties inside the owner object (address, dateOfBirth, and name).

When instantiating a new form control as a first parameter we are providing the value of a control and as a second parameter the Validators array, which holds all the validation rules for our controls.

In the validateControl() method, we are checking if the current control is invalid and touched (we don’t want to show an error if the user didn’t place the cursor inside control at all).  Furthermore, the hasError() function will check which validation rule the current control has violated.

Our dateOfBirth control has the appDatepicker directive, which attaches datepicker inside that input control. Selecting the date won’t patch a value for that control inside the ownerForm, so we need to do that manually inside the executeDataPicker() function.

If you look at the redirectToOwnerList() function, you are going to see the familiar code for navigating back to the previous component. There is another way of doing this by importing the Locationfrom the @angular/common and injecting it inside the constructor and then just calling the back() function on that injected property (location.back()). What you decide to use is totally up to you.

After all, we are extracting values from the ownerForm, and sending a POST request to our server.

Now just modify our root CSS file (styles.css), to show <em> messages with the red color and the bold style:

Similarly, modify the owner-create.component.css file to wrap the inputs in the red color if they are invalid:

Inspecting Results

Form with required errors:

Input errors Angular Form Validation

Form with additional errors:

additional input errors Angular Reactive Form Validation

Valid form:

valid form Angular Reactive Form Validation

The owner created successfully:

Owner created successfully Angular Form Validation

When you click the OK button, you will be redirected to the owner-list page and the new owner is going to be on the list.

In the ErrorHandler service change the handleOtherError function:

Now if the error other than 500 or 404 appears, we are going to show a modal message to the user:

error modal Angular Form Validation

Conclusion

By reading this post you have learned:

  • About different validation types in the Angular
  • How to use reactive form validation in the HTML file
  • How to use reactive form validation in the component file
  • The way to create a new entity on the client side

Thank you for reading the post, hopefully, it was helpful to you.

In the next part of the series, we are going to write the update part of the project, by sending the POST request towards our server.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published Angular content we encourage you to subscribe to our blog.