
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
angular-formsbuilder-gen
Advanced tools
Accelerate Angular Development with Auto Reactive Forms Builder Generator Based On Open API
Unlock enhanced productivity in Angular development with our innovative NPM module! Seamlessly generate form classes directly from OpenAPI specifications, ensuring adherence to Angular best practices. Our solution is tailored for Angular 12 and above, offering compatibility and reliability.
Save valuable time and resources by automating the creation of form group objects. Say goodbye to manual labor and tedious form building processes. Our tool empowers developers with a FormBuilder Helper class, effortlessly generating FormBuilder scripts for each model. Experience streamlined development workflows and expedited project delivery with our intuitive solution.
Optimize your Angular development workflow today and elevate your productivity to new heights!
To install "angular-formsbuilder-gen" globally or within your project, run the following commands:
npm install -g ng-openapi-gen
npm install -g angular-formsbuilder-gen
Create a configuration file named swagger.json in the root of your Angular app with the following content:
{
"$schema": "node_modules/ng-openapi-gen/ng-openapi-gen-schema.json",
"input": "https://localhost:44325/swagger/v1/swagger.json",
"output": "./src/app/api",
"ignoreUnusedModels": false,
"modelsPath": "./../api/models",
"formsOutput": "/src/app/forms",
"schemeFile": "E://swagger.json"
}
Note: This file is also used by the ng-openapi-gen tool.
Our tool specifically uses the properties modelsPath, formsOutput, and input:
First, generate services and models using ng-openapi-gen :
ng-openapi-gen -c swagger.json
To generate Angular models' FormBuilder classes, execute the following command:
ng-frmGenerator swagger.json
or
ng-frmGenerator
only because default filename for configuration is "swagger.json"
Here is an example of a generated FormBuilder class for a simple user information form:
import { Injectable } from '@angular/core';
import { DatePipe } from '@angular/common';
import { FormControl, FormArray, FormGroup, FormBuilder, AbstractControl } from '@angular/forms';
import { IFormBuilder } from './IFormBuilder';
import { CustomerDto, UserAddressDto } from './../api/models';
import { UserAddressDtoFormBuilder } from './UserAddressDto';
import { oneOfValidator, guidValidator } from './CustomeValidators';
@Injectable({ providedIn: 'root' })
export class CustomerDtoFormBuilder implements IFormBuilder<CustomerDto> {
DatePipe: DatePipe = null as any;
DateFormat: string = 'yyyy-MM-dd';
form: FormGroup = null as any;
constructor(private fb: FormBuilder
, private UserAddressDtoFormBuilderSrvc: UserAddressDtoFormBuilder
) {
this.DatePipe = new DatePipe('en-US');
}
updateCulture(culture: string = 'en-US') {
this.DatePipe = new DatePipe(culture);
}
resetForm() {
this.form.reset();
}
buildForm(model: CustomerDto | null = null) {
this.form = this.fb.group({
userName: [ '' , Validators.compose([ Validators.required, Validators.minLength(1) ]) ],
password: [ '' , Validators.compose([ Validators.required ]) ],
addresses: [ this.UserAddressDtoFormBuilderSrvc.buildForm() ],
});
if (model != null) {
this.form.patchValue({ ...model });
}
return this.form;
}
get userNameCtrl(): FormControl {
return this.form.get('userName') as FormControl;
}
get userNameValueChanges$() {
return this.userNameCtrl?.valueChanges;
}
get passwordCtrl(): FormControl {
return this.form.get('password') as FormControl;
}
get passwordValueChanges$() {
return this.passwordCtrl?.valueChanges;
}
addressesArray(): FormArray {
return this.form.controls['addresses'] as FormArray;
}
addressesControls(): AbstractControl<any, any>[] {
return this.addressesArray().controls;
}
deleteAddressesByIndex(index: number): void {
this.addressesArray().removeAt(index);
}
addNewAddresses(model: UserAddressDtoFormBuilder | null = null): FormGroup<any> {
let frm = this.UserAddressDtoFormBuilderSrvc.buildForm(model);
this.addressesArray().push(frm);
return frm;
}
addNewaddresses(model: UserAddressDto | null = null): FormGroup<any> {
let frm = this.UserAddressDtoFormBuilderSrvc.buildForm(model);
this.addressesArray().push(frm);
return frm;
}
}
---
Core Changes:
.provider()
.@Component({
selector: 'app-example',
templateUrl: './example.component.html',
providers: [CustomerDtoFormBuilder.provider()]
})
export class ExampleComponent {
constructor(private customerDtoFormBuilder: CustomerDtoFormBuilder) {
// Use the form builder service
}
}
The .provider()
method ensures all dependent services are registered correctly, allowing for seamless integration and usage within your components.
Enhanced Functionality for FormArrays:
addressesFormAt(index: number) {
return (this.addressesArray().controls[index] as any).__formGroupManager as UserAddressDtoFormBuilder;
}
addNewAddress(model: UserAddressDto | null = null): FormGroup<any> {
let formInstance = this.InjectorSrvc.get(UserAddressDtoFormBuilder);
let frm = formInstance.buildForm(model);
(frm as any).__formGroupManager = formInstance;
this.addressesArray().push(frm);
return frm;
}
This enhancement provides a structured way to manage nested FormGroups within FormArrays, improving the flexibility and maintainability of your form structures. You can now manage FormArrays using our services, making it easier to handle complex forms in your Angular applications.
<form [formGroup]="myFormGroup">
<!-- Other form elements -->
<div showForError="required" formControlName="controlName">
This will show if 'invalidCharacters' error exists.
</div>
<!-- Other form elements -->
</form>
FAQs
Accelerate Angular Development with Auto Reactive Forms Builder Generator Based On Open API
The npm package angular-formsbuilder-gen receives a total of 0 weekly downloads. As such, angular-formsbuilder-gen popularity was classified as not popular.
We found that angular-formsbuilder-gen demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.