I've been trying many different methods have been failing all week, I need to be able to associate a checkbox control with an answer.
Pretty much, if the checkbox is checked then user must answer the question and it must have a validation of minlength 4.
The checkbox will contain a question and answer.
So if the user chooses that question he/she must provide an answer.
the questions are rendered from the server in an object such as;
{
question_id: "1",
selected: true,
EN: "Question 1 - EN",
FR: "Question 1 -FR",
},
{
question_id: "2",
selected: false,
EN: "Question 2 - EN",
FR: "Question 2 -FR"
}
I can post my code if required, it is however very long and complicated.
If you create a Reactive Form you can dynamically change the validation
component
The
createQuestionControl()
method will change each question into a control as below which the form builder can turn into a group with a question and an answerThe
changeValidator()
method will add or remove validators on the answer if the question is changed (note: do not forgetupdateValueAndValidity
)template
Following a clarification in the comments:
I have updated the array to have cross field validation of no more than 3 (easier to test you can change it to 5)
and you would change the
fb.array
to include the validator functionScreenshot of result
Live plunker example