I have an array with questions.I have to read it and show questions dynamically.So I have few questions related to it.
1) I have no idea how to initialize controllers to FormBuilder instance.
2) how to create questions dynamically
3) how to add validations dynamically
I created this project using angular 8.
Mainly I have 4 types of questions in a survey.
MCQ ( only have to select one answer)
Multiple Select ( User can select multiple answers)
Ranking question ( user have to give the correct order of answers)
Descriptive ( Users own answer can give)
Here is my array with questions
questions: any = [
{
id: 11,
surveyNo: 5,
qNo: 1,
question: 'What is the country you would like to travel?',
qType: 1,
noAnswrs: 4,
answerType: 1,
answrs: ['America', 'Australia', 'India', 'England']
},
{
id: 12,
surveyNo: 5,
qNo: 2,
question: 'What type of credit cards do you have?',
qType: 2,
noAnswrs: 4,
answerType: 1,
answrs: ['Visa', 'Mastercard', 'American Express', 'Discover']
},
{
id: 13,
surveyNo: 5,
qNo: 3,
question: 'Please rank the following features in order of importance,where 1 is the most important to you.?',
qType: 3,
noAnswrs: 4,
answerType: 1,
answrs: ['Location', 'Confort', 'Service', 'Value for money']
},
{
id: 14,
surveyNo: 5,
qNo: 4,
question: 'What is your idea about our institute?',
qType: 4,
noAnswrs: 0,
answerType: 1,
answrs: []
}];
here is html code
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header bg-transparent border-success">
<h3>15 questions</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-12">
<form [formGroup]="surveyQuestionForm">
<div class="form-group">
<label class="control-label"> 1) What is the country you would like to
travel?</label>
<div class="ml-3">
<table>
<th width="auto"></th>
<th width="auto"></th>
<tr>
<td>1. America</td>
<td>
<div class=" custom-radio custom-control">
<input type="radio" class="custom-control-input" id="q1_1"
name="q1" value="1" formControlName="q1" />
<label class="custom-control-label" for="q1_1">
</label>
</div>
</td>
</tr>
<tr>
<td>2. Australia </td>
<td>
<div class=" custom-radio custom-control">
<input type="radio" class="custom-control-input" id="q1_2"
name="q1" value="2" formControlName="q1" />
<label class="custom-control-label" for="q1_2"></label>
</div>
</td>
</tr>
<tr>
<td>3. India </td>
<td>
<div class="custom-radio custom-control">
<input type="radio" class="custom-control-input" id="q1_3"
name="q1" value="3" formControlName="q1" />
<label class="custom-control-label" for="q1_3"></label>
</div>
</td>
</tr>
<tr>
<td>4. England </td>
<td>
<div class=" custom-control custom-radio">
<input type="radio" class="custom-control-input" id="q1_4"
name="q1" value="4" formControlName="q1" />
<label class="custom-control-label" for="q1_4"></label>
</div>
</td>
</tr>
</table>
</div>
</div>
<div class="form-group">
<label class="control-label"> 2) What type of credit cards do you have?</label>
<div class="ml-3">
<table>
<th width="auto"></th>
<th width="auto"></th>
<tr>
<td>1. Visa </td>
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="q2_1"
value="1" formControlName="q2" />
<label class="custom-control-label" for="q2_1"></label>
</div>
</td>
</tr>
<tr>
<td>2. Mastercard</td>
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="q2_2"
value="2" formControlName="q2" />
<label class="custom-control-label" for="q2_2"></label>
</div>
</td>
</tr>
<tr>
<td>3. American Express</td>
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="q2_3"
value="3" formControlName="q2" />
<label class="custom-control-label" for="q2_3"></label>
</div>
</td>
</tr>
<tr>
<td>4. Discover</td>
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="q2_4"
value="4" formControlName="q2" />
<label class="custom-control-label" for="q2_4"></label>
</div>
</td>
</tr>
</table>
</div>
</div>
<div class="form-group">
<label class="control-label"> 3) Please rank the following features in order of importance,where 1 is the most important to you.?</label>
<div class="ml-3">
<table>
<tr>
<td>1. Location </td>
<div class="invalid-feedback"
*ngIf="surveyQuestionForm.get('q3').touched && surveyQuestionForm.get('q3').hasError('required')">Answer required</div>
<div class="invalid-feedback"
*ngIf="surveyQuestionForm.get('q3').touched && surveyQuestionForm.get('q3').hasError('max')">max value</div>
<td><input type="number" style="width:40px;" id="q3_1"
[ngClass]="{'is-invalid': surveyQuestionForm.get('q3').errors && surveyQuestionForm.get('q3').touched}"
formControlName="q3" class="text-center" /></td>
</tr>
<tr>
<td>2. Confort </td>
<td><input type="number" style="width:40px;" id="q3_1"
class="text-center" /></td>
</tr>
<tr>
<td>3. Service </td>
<td><input type="number" style="width:40px;" id="q3_1"
class="text-center" /></td>
</tr>
<tr>
<td>4. Value for money </td>
<td><input type="number" style="width:40px;" id="q3_1"
class="text-center" /></td>
</tr>
</table>
</div>
</div>
<div class="form-group">
<label class="control-label"> 4) What is your idea about our institute?</label>
<div class="ml-3">
<table>
<th width="auto"></th>
<th></th>
<tr>
<td><textarea class="form-control" rows="5" id="comment" name="text"
formControlName="q4"></textarea></td>
</tr>
</table>
</div>
</div>
</form>
</div>
</div>
<div class="row">
<div class="col-md-12">
<button class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
here is typescript code
surveyQuestionForm: FormGroup;
constructor(private fb: FormBuilder) { }
questions: any = [
{
id: 11,
surveyNo: 5,
qNo: 1,
question: 'What is the country you would like to travel?',
qType: 1,
noAnswrs: 4,
answerType: 1,
answrs: ['America', 'Australia', 'India', 'England']
},
{
id: 12,
surveyNo: 5,
qNo: 2,
question: 'What type of credit cards do you have?',
qType: 2,
noAnswrs: 4,
answerType: 1,
answrs: ['Visa', 'Mastercard', 'American Express', 'Discover']
},
{
id: 13,
surveyNo: 5,
qNo: 3,
question: 'Please rank the following features in order of importance,where 1 is the most important to you.?',
qType: 3,
noAnswrs: 4,
answerType: 1,
answrs: ['Location', 'Confort', 'Service', 'Value for money']
},
{
id: 14,
surveyNo: 5,
qNo: 4,
question: 'What is your idea about our institute?',
qType: 4,
noAnswrs: 0,
answerType: 1,
answrs: []
}
];
ngOnInit() {
this.createForms();
}
createForms(): any {
this.surveyQuestionForm = this.fb.group({
q1: ['', [Validators.required]],
q2: ['', [Validators.required]],
q3: ['', [Validators.required, Validators.min(1), Validators.max(3)]],
q4: ['', [Validators.required]]
});
}
Here are the validators have to use
- required validator
- min value(1) and max value(4)
- have to check already inserted value inserted or not (no idea how to do this dynamically)
I red many articles,but those are not in dynamically create. Please help me to do this
thanks
