jlf-typed-form-craft
Version:
A library to create forms with typed datas and validations
124 lines (98 loc) • 3.24 kB
Markdown
# FormGroup Decorator Library Documentation
[](https://badge.fury.io/js/jlf-typed-form-craft)
[](https://www.npmjs.com/package/jlf-typed-form-craft)
## Overview
This library provides a convenient way to transform TypeScript classes into Angular FormGroups using decorators. It simplifies form management by automatically creating FormControls from class properties and handling validation.
## Installation
npm install jlf-typed-form-craft
## Requirements
- Angular 19.0.0 or higher
- TypeScript 5.0.0 or higher
## Features
### Automatic FormGroup Generation
- Transform any class into a FormGroup using the `` annotation
- Class properties are automatically converted into FormControls
- Supports nested forms and complex structures
Example:
```typescript
// Define your class with the decorator
export class MyClass {
firstField: string;
secondField: boolean;
anotherField: number;
fieldToIgnore: string;
}
// Usage in component
export class CustomerComponent {
customerForm: FormGroup;
constructor() {
const model = new MyClass();
// Automatically creates a FormGroup with all FormControls (only decorated)
// with default value set
this.customerForm = createFormFromClass<MyClass>(model);
}
}
```
### Built-in Validation
- Includes `JlValidators` for common validation scenarios
- Validators can be activated based on conditions
- Easy to combine multiple validators
Example:
```typescript
// Define your class with the decorator
export class MyClass {
// add two validators
firstField: string;
secondField: boolean;
// added JlValidators with conditional expression.
anotherField: number;
fieldToIgnore: string;
}
```
Other JlValidators available, working in the same way:
* minLength
* maxLength
* pattern
* email
* min
* max
* requiredTrue
### Conditional Form Control Management
- Enable/disable form controls based on custom conditions
- Dynamic validation based on form state
- Reactive form control behavior
Example:
```typescript
// Define your class with the decorator
export class MyClass {
firstField: string;
// added disable control if firstField invalid
secondField: boolean;
anotherField: number;
fieldToIgnore: string;
}
```
## Licence
MIT