An open API service indexing awesome lists of open source software.

https://github.com/bobbyg603/ngx-confirm-password-example

📐🦸📋 Angular custom validator example that ensures the values of two form controls are equal
https://github.com/bobbyg603/ngx-confirm-password-example

angular error-tailor forms ng-neat reactive-forms

Last synced: about 2 months ago
JSON representation

📐🦸📋 Angular custom validator example that ensures the values of two form controls are equal

Awesome Lists containing this project

README

          

## 📐📋🦸 Angular Super Forms: Confirm Password

Angular Super Forms Confirm Password

[![medium profile link](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/p/bd95906f220f)
[![twitter profile link](https://img.shields.io/badge/Twitter-1DA1F2?style=for-the-badge&logo=twitter&logoColor=white)](https://twitter.com/bobbyg603/status/1689632582994907136)
[![StackBlitz](https://img.shields.io/badge/StackBlitz-Edit-blue?style=for-the-badge&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC4AAABECAYAAAD+1gcLAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAB3RJTUUH5AINBw4X0bTGRQAABSxJREFUaN7VmVtsFFUYx//fmQW79bbd2QKpaIIaDcGoifFBEgMGqTTRRA01SgxE5Rbi7QG6S3lgo9J2twpeotxEQlCigLdoQwJ4ARN9QB9MRCNRDBdRzE7LJbTSmTl/H4BYStmd2Z3tDOdt5lzml/9833fO9x0gYi2xgom6Tt5aapyKEnRDlrVGPzfGT+G3SwZ87HLGT8f5uYD7jmSl99IAX80RfTY3A5wMqDVepoQPnqVKHtMbAN4PyJeFtPwafXBSknG9UoDHAIDQq7xODRU8mdc5Aeaeffy7O2F8GnnwZM5dKsCic88CrMU8sSMNbubdZwTIDnjlOoZa52eNYQc3c84sEK+d/1a6ji2UA5EFN3POw4C8fcYy/m+a3p1y2MGTOXsqIJsAxAZ1Hei53tgeSfBkBycK1McALrswJGIVHhE3cuD1ed4uorsAXD5Ed7/hqvXlrFtV8LpO3qKpdwJIDLn/AB/+s0SORgp8VJ43KK23AzAvNsagWlXu+lKV6LGc14itvyEwrsiw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EtoVRDvDsnMlXeK04+o+ztRgSnNOdjq1DSM2z4uLoeecKSCQWhgntXfEsY2ZcHwDQAMESq8VoC7ty5EnxZK37EIAGAV6NArT3c3def2Hm3HdASlSYSipe384bAR6x+tTsIBOBqoMTzlirVz2BrOgoWcF/mizikfkwKiQAAAAASUVORK5CYII=)](https://stackblitz.com/edit/github-ktbxdp-ftgv1g)

This is is a companion repo for [Angular Super Forms: Confirm Password](https://medium.com/p/bd95906f220f) that demonstrates how to build a confirm password form with [ng-bootstrap](https://ng-bootstrap.github.io/#/home), [error-tailor](https://github.com/ngneat/error-tailor), c[omponent validation](https://medium.com/p/bd95906f220f#c02e), [styling](https://medium.com/p/bd95906f220f#b7b4), and [form validation](https://medium.com/p/bd95906f220f#e1f3). Topics in this article include how to configure error-tailor for professional looking errors, validate individual fields, as well as creating a form that validates two components have the same value.

## ☕️ TL;DR

Clone this repo to your workspace:

```sh
git clone https://github.com/bobbyg603/ngx-confirm-password-example
```

Install the dependencies and start the application:

```sh
cd ngx-confirm-password-example && npm i && npm start
```

Enter invalid values into the form and click away from the inputs. Also enter a value for new password that doesn't match the value for confirm password before hitting submit. You should see several field validation errors as well as a form validation error at the bottom.

Angular Super Forms Preview

## 🕵️ Inspecting the Code

There are a few things in [change-password-form.component.ts](https://github.com/bobbyg603/ngx-confirm-password-example/blob/main/src/app/change-password-form/change-password-form.component.ts) that are worth mentioning.

The first thing to notice is [`ControlsOf`](https://github.com/bobbyg603/ngx-confirm-password-example/blob/4fe68dd3255808e13742b573437987fb25c9b697/src/app/change-password-form/change-password-form.component.ts#L14C26-L14C26). The `ControlsOf` utility type allows us to convert an interface’s string types to `FormControl` types. This allows us to define the data model with a single interface and ensure that our form adheres to the same interface.

Here’s what `ControlsOf` evaluates to:

```ts
interface ControlsOf {
currentPassword: FormControl;
newPassword: FormControl;
confirmPassword: FormControl;
}
```

More info on the `ControlsOf` utility type can be found [here](https://betterprogramming.pub/how-to-build-a-strongly-typed-angular-14-super-form-86837965a0e5).

The next piece worth noting is that we’re injecting NonNullableFormBuilder into the constructor. We use the [`NonNullableFormBuilder`](https://angular.io/guide/typed-forms#formbuilder-and-nonnullableformbuilder) because otherwise, we get compiler errors that `FormControl` cannot be assigned to an object expecting the type `FormControl`.

```ts
constructor(formBuilder: NonNullableFormBuilder) { ... }
```

Finally, we’ve also created an array of validators that we’re passing to each of the form controls.

```ts
const validators = [Validators.required, Validators.minLength(8), Validators.pattern(upperLowerSymbolNumberRegex)];

const currentPassword = formBuilder.control('', validators);
const newPassword = formBuilder.control('', validators);
const confirmPassword = formBuilder.control('', validators);
```

Notice that we’re using `Validators.pattern(upperLowerSymbolNumberRegex)`. This regular expression was found on [Stack Overflow](https://stackoverflow.com/a/1559788/2993077), and we created a `const` with a descriptive name so that the next developer to work on the code can more easily understand the purpose of the complicated regular expression.

Last but not least, let's take a look at the template in [change-password-form.component.html](https://github.com/bobbyg603/ngx-confirm-password-example/blob/main/src/app/change-password-form/change-password-form.component.html).

```html






Change Password

Fill in the form below and click submit to update your password.






Submit






```

Notice that on the very end of the `` tag we’ve added the `errorTailor` directive. The `errorTailor` directive makes validation messages magically appear when a form control has been changed and unfocused.

## 🧑‍💻 Next Steps

If you liked this example, please follow me on [Medium](https://bobbyg603.medium.com/) and [X](https://twitter.com/bobbyg603), where I post programming tutorials and discuss tips and tricks that have helped make me a better programmer.

**Thank you for your support ❤️**