Skip to content

Commit

Permalink
feat(validator): add new noWhitespace validator function
Browse files Browse the repository at this point in the history
  • Loading branch information
truonghungit committed Nov 6, 2023
1 parent e5f99e3 commit ccf9840
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Created by Angular developer for Angular developers with ❤️.
- [Demo](https://ngx-validator-showcase.vercel.app/)
- [Quick Start](#quick-start)
- [Validation Messages](#validation-messages)
- [When Does Validation Run](#when-does-validation-run)
- [Validators](#validators)
- [Changelog](#changelog)
- [License](#license)
Expand Down Expand Up @@ -353,7 +354,7 @@ function color(color: string): ValidatorFn {
wrongColor: {
requiredColor: color,
actual: control.value,
message: 'Invalid color'
message: 'Invalid color',
},
};
};
Expand All @@ -362,6 +363,34 @@ function color(color: string): ValidatorFn {

[Back to top](#table-of-contents)

## When Does Validation Run

By default, ngx-validator will run validation method when:

#### Form control dirty & touched

> A controls is `dirty` if users have changed the value of the control on UI.
>
> A controls is `touched` if users have focused on the control and then focused on something else.
> For example by clicking into the control and then pressing tab or clicking on another control in the form.
#### OR Whenever users attempt to submit a form.

You can control when ngx-validator runs validation by changing the configuration depending on your needs.

```typescript
const formValidatorConfig: Partial<FormValidatorConfig> = {
validateOn: (status) => {
return status.touched || status.submited;
},
};

@NgModule({
imports: [FormValidatorModule.configure(formValidatorConfig)],
})
export class AppModule {}
```

## Validators

### Angular built-in validators
Expand Down
6 changes: 6 additions & 0 deletions projects/ngx-validator/src/lib/validators/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,9 @@ export function equalTo(controlNamePath: string, message?: string): ValidatorFn
: { equalTo: { requiredValue: equalControl.value, actual: control.value, message } };
}
}

export function noWhitespace(message?: string): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
return (control.value || '').trim().length ? null : { whitespace: { message } };
};
}

0 comments on commit ccf9840

Please sign in to comment.