📦 Installation
Step 1: Install Angular Formly
npm install @ngx-formly/core @ngx-formly/bootstrap
Step 2: Import in your module
import { FormlyModule } from '@ngx-formly/core';
import { FormlyBootstrapModule } from '@ngx-formly/bootstrap';
@NgModule({
imports: [
ReactiveFormsModule,
FormlyModule.forRoot(),
FormlyBootstrapModule
]
})
export class AppModule { }
🔰 Basic Usage
Component Setup
import { Component } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFormOptions, FormlyFieldConfig } from '@ngx-formly/core';
@Component({
selector: 'app-example',
template: `
<form [formGroup]="form" (ngSubmit)="onSubmit(model)">
<formly-form [form]="form" [fields]="fields" [model]="model"></formly-form>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
`
})
export class AppComponent {
form = new FormGroup({});
model = { email: '
[email protected]' };
fields: FormlyFieldConfig[] = [
{
key: 'email',
type: 'input',
templateOptions: {
label: 'Email address',
placeholder: 'Enter email',
required: true,
}
}
];
onSubmit(model: any) {
console.log(model);
}
}
🎛️ Field Types
Angular Formly supports various field types out of the box:
Input Fields
Text, email, password, number, tel, url
Selection Fields
Select dropdown, radio buttons, checkboxes
Text Areas
Multi-line text input for longer content
Custom Fields
Create your own field types for specific needs
✅ Validation
Built-in Validators
fields: FormlyFieldConfig[] = [
{
key: 'email',
type: 'input',
templateOptions: {
label: 'Email',
required: true,
},
validators: {
validation: ['email'],
},
},
{
key: 'password',
type: 'input',
templateOptions: {
label: 'Password',
type: 'password',
minLength: 6,
required: true,
}
}
];
Custom Validators
export function EmailValidator(control: AbstractControl): ValidationErrors | null {
return /\S+@\S+\.\S+/.test(control.value) ? null : { 'email': true };
}
// Register validator
FormlyModule.forRoot({
validators: [
{ name: 'email', validation: EmailValidator },
],
})
🔧 Troubleshooting
Common Issues
Form Not Displaying
Make sure you've imported ReactiveFormsModule and FormlyModule in your app module
Validation Not Working
Check that your field configuration includes proper validation rules and error messages
Styling Issues
Ensure you've imported the correct UI library (Bootstrap, Material, etc.)
Model Binding Problems
Verify that your model object keys match the field keys in your configuration
📚 Additional Resources
For more comprehensive documentation and examples:
- 🌐 Official Documentation: formly.dev
- 📖 GitHub Repository: github.com/ngx-formly/ngx-formly
- 💬 Community Forum: Join the discussions on GitHub Issues
- 🎥 Video Tutorials: Available on YouTube and other platforms
- 📦 NPM Package: npmjs.com/package/@ngx-formly/core