Reactive forms validation for pros
I very much missed the ng-messages
directive from AngularJS, so I created a similar set of directives to use in Angular 2+.
In contrast to the directives from AngularJS, the directives in this library require passing the control name to the directive, instead of the control's errors.
This allowed me to hook into the status of control, such as its dirty
state, and display validation messages according to that status.
The design of this library promotes less boilerplate code, which keeps your templates clean.
- ✅ Simple syntax that reduces boilerplate
- ✅ Configure when to display error messages for an app further reducing boilerplate
- ✅ Seamless integration with Reactive Forms
- ✅ Works with nested forms
npm install @ngspot/ngx-errors
yarn add @ngspot/ngx-errors
Import library into application module:
import { NgxErrorsModule } from '@ngspot/ngx-errors'; // <-- import the module
@NgModule({
imports: [
NgxErrorsModule, // <-- include imported module in app module
],
})
export class MyAppModule {}
@Component({
selector: 'my-component',
template: `
<form [formGroup]="myForm">
<input formControlName="email" type="email" />
<div ngxErrors="email">
<div ngxError="required">Email is required</div>
</div>
</form>
`,
})
export class MyComponent implements OnInit {
myForm: FormGroup;
constructor(private fb: FormBuilder) {
this.myForm = this.fb.group({
email: ['', Validators.required],
});
}
}
@Component({
selector: 'my-component',
template: `
<input [formControl]="email" placeholder="Email" type="email" />
<div [ngxErrors]="email">
<div ngxError="required">Email is required</div>
</div>
`,
})
export class MyComponent implements OnInit {
email = new FormControl('', Validators.required);
}
Configure when to show messages for whole module by using .configure()
method:
@NgModule({
imports: [
NgxErrorsModule.configure({ ... }) // <- provide configuration here
],
})
export class MyAppModule {}
Alternatively, use dependency injection to provide configuration at a component level:
import { ErrorsConfiguration } from '@ngspot/ngx-errors';
const myConfig = { ... }; // <- specify config
@Component({
...
providers: [
{ provide: ErrorsConfiguration, useValue: myConfig }
]
})
export class MyComponent { }
Here's the configuration object interface:
interface IErrorsConfiguration {
/**
* Configure errors to show only when the corresponding input is dirty.
*
* Default is `true`.
*/
showErrorsOnlyIfInputDirty?: boolean;
/**
* Configure errors to show only when form is submitted.
* Upon form submission shows errors even if `showErrorsOnlyIfInputDirty = true`
* and some of the inputs aren't dirty.
* Takes effect only when ngxErrors directive is a child of a form.
*
* Default is `false`.
*/
showErrorsWhenFormSubmitted?: boolean;
}
Include something similar to the following in global CSS file:
[ngxerrors] {
color: red;
}
- Develop
- Write specs
- Run
npm run test:lib
- Run
npm run commit
and choose fix or feature - Run
npm run release
- Run
npm run build:lib
- Go to the dist directory and run
npm publish
build:lib
- Builds the librarytest:lib
- Runs teststest:lib:headless
- Runs tests in headless mode with Chromerelease
- Releases a new version; this will bump the library's version and update theCHANGE_LOG
file based on the commit messagerelease:first
- Creates the first releasecommit
- Creates a new commit message based on Angular commit messgae conventioncontributors:add
- Adds a new contributor to theREADME
file
MIT © Dmitry Efimenko
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!