|
| 1 | + |
| 2 | + |
1 | 3 | # Angular South African Form Validators |
2 | 4 |
|
3 | 5 | A set of South African specific validators for Angular forms. |
4 | 6 |
|
5 | | -TODO: Add the Runninghill and Angular logos as well as some emojis |
6 | | - |
7 | | -# Getting Started |
| 7 | +# Getting Started🏁 |
8 | 8 |
|
9 | 9 | If you plan on contributing to the package please read the contributor readme. |
10 | 10 |
|
11 | 11 | 1. Run `npm install rh-angular-rsa-form-validators` in your Angular project. |
12 | 12 |
|
13 | 13 | 2. Import and use the exposed classes to validate your Angular form controls. |
14 | 14 |
|
15 | | -# Usage |
| 15 | +# Usage💡 |
| 16 | + |
| 17 | +To begin validating your South African specific form fields, start by importing |
| 18 | +the _RsaFormValidators_ class. |
| 19 | + |
| 20 | +```Javascript |
| 21 | +import { RsaFormValidator } from 'rh-angular-rsa-form-validators' |
| 22 | +``` |
| 23 | + |
| 24 | +Add the appropriate validator to your form control(s). |
| 25 | + |
| 26 | +```Javascript |
| 27 | +// When creating your form |
| 28 | +this.form = this.formBuilder.group({ |
| 29 | + name: ['', Validators.required], |
| 30 | + mobileNumber: ['', [Validators.required, RsaFormValidator.phoneNumber(false, true)]], |
| 31 | + workNumber: [''] |
| 32 | +}) |
| 33 | + |
| 34 | +// After the form is already created |
| 35 | +this.form.controls['workNumber'].setValidators([RsaFormValidators.phoneNumber(false, true)]) |
| 36 | +``` |
| 37 | + |
| 38 | +To determine if the form control passed the validation, check the errors object on the form control. |
| 39 | + |
| 40 | +RsaFormValidator error objects on form controls will always be the name of the validator, |
| 41 | +prefixed with 'rsa' eg; for the rsa phone number validator, the error object is `rsaPhoneNumber` |
| 42 | +(rsa + phoneNumber = rsaPhoneNumber) |
| 43 | + |
| 44 | +```Javascript |
| 45 | +if (this.form.controls['mobileNumber'].errors.rsaPhoneNumber) |
| 46 | + errorMessage = 'Mobile number must be a valid South African phone number.' |
| 47 | +``` |
| 48 | + |
| 49 | +# API Reference📖 |
| 50 | + |
| 51 | +### RsaFormValidator |
| 52 | + |
| 53 | +Contains form control validators used to validate South African specific form fields. |
| 54 | + |
| 55 | +[_source code_](https://github.com/Runninghill/rh-angular-rsa-form-validators/blob/main/projects/rh-rsa-form-validators/src/lib/validators/rsa-form-validator.ts) |
| 56 | + |
| 57 | +#### Methods |
| 58 | + |
| 59 | +##### phoneNumber(allowCountryCode, allowSpaces) |
16 | 60 |
|
17 | | -TODO: Fill out usage section |
| 61 | +Returns a validator which determines if a form control is a valid South African phone number. |
18 | 62 |
|
19 | | -# API Reference |
| 63 | +Parameters |
20 | 64 |
|
21 | | -TODO: Fill out api reference section |
| 65 | +|Name|Type|Description|Default| |
| 66 | +|----|----|-----------|-------| |
| 67 | +|allowCountryCode|boolean|determines if the phone number is allowed to contain the South African country code (+27)|true| |
| 68 | +|allowSpaces|boolean|determines if a phone number is allowed to contain spaces|true| |
0 commit comments