Angular Form for Services with File Uploads
Here's how you can create an Angular form with file upload fields for each service, including the special cases for Journal Submission and Conference:
1. First, define your form model in your component:
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-service-form',
templateUrl: './service-form.component.html',
styleUrls: ['./service-form.component.css']
})
export class ServiceFormComponent {
serviceForm: FormGroup;
services = [
"Thesis Writing", "Dissertation Help", "Research Proposal", "Grammar Check",
"Formatting", "Plagiarism Check", "Plagiarism Removal", "Paper Writing",
"Journal Submission", "Synopsis Writing", "PowerPoint Presentation", "Implementation Help",
"Conference", "Pre Viva PPT", "Main Viva PPT", "ALL Work Shop", "Literature Review",
"Patent Assistance", "Book Chapter Writing", "Thesis Presentation", "Viva Prep"
];
constructor(private fb: FormBuilder) {
this.serviceForm = this.fb.group({
thesisWriting: [null],
dissertationHelp: [null],
researchProposal: [null],
grammarCheck: [null],
formatting: [null],
plagiarismCheck: [null],
plagiarismRemoval: [null],
paperWriting: [null],
journalSubmission: this.fb.group({
scopus: [null],
ugc: [null],
paperReview: this.fb.group({
journalName: [''],
issueNo: [''],
publicationMonthYear: ['']
})
}),
synopsisWriting: [null],
powerPointPresentation: [null],
implementationHelp: [null],
conference: this.fb.group({
conferenceName: [''],
certificationName: ['']
}),
preVivaPPT: [null],
mainVivaPPT: [null],
allWorkShop: [null],
literatureReview: [null],
patentAssistance: [null],
bookChapterWriting: [null],
thesisPresentation: [null],
vivaPrep: [null]
});
}
onSubmit() {
if (this.serviceForm.valid) {
console.log(this.serviceForm.value);
// Handle form submission
}
}
}
2. Create the template (HTML) for the form:
<form [formGroup]="serviceForm" (ngSubmit)="onSubmit()">
<div class="form-container">
<!-- Regular services with file uploads -->
<div *ngFor="let service of services" class="form-group">
<label>{{service}}</label>
<!-- Special handling for Journal Submission -->
<div *ngIf="service === 'Journal Submission'" formGroupName="journalSubmission">
<div class="nested-form">
<h4>Journal Submission Details</h4>
<div class="form-group">
<label>Scopus Upload</label>
<input type="file" formControlName="scopus">
</div>
<div class="form-group">
<label>UGC Upload</label>
<input type="file" formControlName="ugc">
</div>
<div formGroupName="paperReview">
<h5>Paper Review Details</h5>
<div class="form-group">
<label>Journal Name</label>
<input type="text" formControlName="journalName" class="form-control">
</div>
<div class="form-group">
<label>Issue Number</label>
<input type="text" formControlName="issueNo" class="form-control">
</div>
<div class="form-group">
<label>Publication Month/Year</label>
<input type="text" formControlName="publicationMonthYear" class="form-control">
</div>
</div>
</div>
</div>
<!-- Special handling for Conference -->
<div *ngIf="service === 'Conference'" formGroupName="conference">
<div class="nested-form">
<h4>Conference Details</h4>
<div class="form-group">
<label>Conference Name</label>
<input type="text" formControlName="conferenceName" class="form-control">
</div>
<div class="form-group">
<label>Certification Name</label>
<input type="text" formControlName="certificationName" class="form-control">
</div>
</div>
</div>
<!-- Regular file upload for other services -->
<div *ngIf="service !== 'Journal Submission' && service !== 'Conference'">
<input type="file" [formControlName]="camelCase(service)">
</div>
</div>
<button type="submit" class="btn btn-primary" [disabled]="!serviceForm.valid">Submit</button>
</div>
</form>
3. Add the camelCase helper function to your component:
// Add this method to your component class
camelCase(str: string): string {
return str.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
}
4. Add some basic CSS (optional):
.form-container {
display: flex;
flex-direction: column;
gap: 20px;
padding: 20px;
}
.form-group {
display: flex;
flex-direction: column;
gap: 5px;
margin-bottom: 15px;
}
.nested-form {
border: 1px solid #ccc;
padding: 15px;
border-radius: 5px;
margin-bottom: 15px;
}
.nested-form h4, .nested-form h5 {
margin-bottom: 10px;
}
button {
margin-top: 20px;
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
Notes:
- The form uses nested FormGroups for Journal Submission and Conference as they have multiple fields.
- For Journal Submission, there are:
- Two file uploads (Scopus and UGC)
- Three text fields for paper review details
- For Conference, there are two text fields (Conference Name and Certification Name)
- All other services have a simple file upload field.
- The
camelCase function converts service names to camelCase for form control names.
- You may need to add proper validation based on your requirements.
- For file uploads, you'll need to handle the file objects separately in your submission logic.
Remember to import the necessary Angular modules in your module file:
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
ReactiveFormsModule,
// other imports
]
})