Applications often require forms that collect visitor information. Some applications require more than a single form, and it is tedious to repeat the HTML code for each form, especially if they are similar forms.
This post will show how you can build and duplicate dynamic forms using Angular Formly and save development time and effort.
Step 1: Install the Formly library
ng add @ngx-formly/schematics --ui-theme=bootstrap
The ui-theme is an optional flag. The other options are,
- bootstrap
- material
- ionic
- primeng
- kendo
- nativescript
After the installation, you need to import the module,
@NgModule({
imports: [
FormlyModule.forRoot(),
FormlyBootstrapModule // if boostrap option is selected
FormlyMaterialModule,// if material option is selected
],
})
We need the forRoot() at the application root level. This method accepts the passing of a config argument as an extra config. The extra config may contain values that define how to register the custom field type, validation for the form, etc.
Step 2: Add Formly inside your component
<form [formGroup]="form">
<formly-form [form]="form" [fields]="fields" [model]="model"></formly-form>
</form>
The <formly-form> component is the main container of the form, that will build and render the form fields. It accepts the following inputs:
- fields: The field configurations for building the form.
- form: The form instance which allows tracking model value and validation status.
- model: The model to be represented by the form.
Step 3: Configure the form
form = new FormGroup({});
model = {};
fields: FormlyFieldConfig[] = [
{
key: 'name',
type: 'input',
templateOptions: {
label: 'Your Name',
placeholder: 'Enter your full name',
required: true,
}
}
];
- key: form control name
- type: type of the field like input, select, checkbox, etc
- templateOptions:
- label: Label for the fields
- placeholder: Placeholder for the field (optional)
- required: boolean (optional)
Validation
In the above code snippet, we see that the Formly library also supports additional validation such as min, max, minLength, maxLength, etc. We can also do custom validation. To do so, we need to include the validators property in the form fields.
validators: {
name: {
expression: c => !c.value || c.value.length <= 10,
message: (error, field: FormlyFieldConfig) =>
'Label should be less than 10 character'
}
}
Use Of Observable
When using the forms, we have to use the select box and show the options for it. We may need to hard code the option values but may also fetch these values from the API. Here we will see how to fetch the values from the API.
fields: FormlyFieldConfig[] = [
{
key: 'sport',
type: 'select',
templateOptions: {
label: 'Sport',
options: this.dataService.getSports(),
valueProp: 'id',
labelProp: 'name',
},
},
];
constructor(private dataService: DataService) {}
In the above code, FormlyFieldConfig is for the field configurations for building the form. In the constructor, we must inject the service.
In the service.ts file, we have to add the method,
sports = [
{ id: '1', name: 'Soccer' },
{ id: '2', name: 'Basketball' },
];
getSports(): Observable<any[]> {
return of(this.sports);
}
Conclusion
Formly helps create and duplicate forms in our application. Validation and use of Observables are the most crucial things while developing the forms and Formly can handle these easily.
Here are some blogs you may enjoy
“Synerzip team is very responsive & quick to adopt new technologies. Team naturally follows best practices, does peer reviews and delivers quality output, thus exceeding client expectations.”
“Synerzip’s agile processes & daily scrums were very valuable, made communication & time zone issues work out successfully.”
“Synerzip’s flexible and responsible team grew to be an extension to the StepOne team. Typical concerns of time zone issues did not exist with Synerzip team.”
“Synerzip worked in perfect textbook Agile fashion – releasing working demos every two weeks. Though aggressive schedules, Synerzip was able to deliver a working product in 90 days, which helped Zimbra stand by their commitment to their customers.”
“Outstanding product delivery and exceptional project management, comes from DNA of Synerzip.”
“Studer product has practically taken a 180% turn from what it was, before Synerzip came in. Synerzip cost is very reasonable as compared to the work they do.”
“Synerzip makes the timezone differences work FOR the customer, enabling a positive experience for us. ‘Seeing is believing’, so we decided to give it a shot and the project was very successful.”
“The Synerzip team seamlessly integrates with our team. We started seeing results within the first sprint. And due to the team’s responsiveness, we were able to get our product to the sales cycle within 7 months.”
“Product management team from Synerzip is exceptional and has a clear understanding of Studer’s needs. Synerzip team gives consistent performance and never misses a deadline.”
“Synerzip is different because of the quality of their leadership, efficient team and clearly set methodologies. Studer gets high level of confidence from Synerzip along with significant cost advantage of almost 50%”
“Synerzip’s hiring approach and practices are worth applauding. Working with Synerzip is like
“What you see is what you get”.”
“Synerzip has dedicated experts for every area. Synerzip helped Tangoe save a lot of cost, still giving a very high quality product.”
“Synerzip gives tremendous cost advantage in terms of hiring and growing the team to be productive verses a readymade team. Synerzip is one company that delivers “co –development” to the core!”
“Synerzip is a great company to work with. Good leadership and a warm, welcoming attitude of the team are additional plus points.”
“Our relationship with Synerzip is very collaborative, and they are our true partners as our values match with theirs.”
“Synerzip has proven to be a great software product co-development partner. It is a leader because of its great culture, its history, and its employee retention policies. ExamSoft’s clients are happy with the product, and that’s how ExamSoft measures that all is going well.”
“They possess a great technical acumen with a burning desire to solve problems. The team always takes the initiative and ownership in all the processes they follow. Synerzip has played a vital role in our scaling up and was a perfect partner in cost, efficiency, and schedules.”
“As we are a startup, things change on a weekly basis, but Synerzip team has been flexible in adapting the same”
“Synerzip team has been very proactive in building the best quality software, bringing in best practices, and cutting edge innovation for our company.”
“We’ve been working for more than six years with Synerzip and its one of the better, if not the best, experience I’ve had working with an outsourcing company.”
“My experience with Synerzip is that they have the talent. You throw a problem at them, and someone from that team helps to solve the issue.”
“The breadth and depth of technical abilities that Synerzip brings on the table and the UX work done by them for this project exceeded my expectations!”
“Synerzip UX designers very closely represent their counterparts in the US in terms of their practice, how they tackle problems, and how they evangelize the value of UX.”
“Synerzip team understood the requirements well and documented them to make sure they understood them rightly.”
“Synerzip is definitely not a typical offshore company. Synerzip team is incredibly communicative, agile, and delivers on its commitments.”
“Working with Synerzip helped us accelerate our roadmap in ways we never thought possible!”
“While working with Synerzip, I get a feeling of working with a huge community of resources, who can jump in with the skills as needed.”