将复选框控件与文本框控件关联的最佳方式 [英] Best way to associate checkbox control with textbox control

查看:19
本文介绍了将复选框控件与文本框控件关联的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试许多不同的方法,但整个星期都失败了,我需要能够将复选框控件与答案相关联.

差不多,如果选中该复选框,则用户必须回答问题,并且必须验证 minlength 4.

复选框将包含一个问题和答案.

因此,如果用户选择该问题,他/她必须提供答案.

问题从服务器呈现在一个对象中,例如;

<代码> {question_id: "1",选择:真实,CN: "问题 1 - CN",FR:问题 1 -FR",},{question_id: "2",选择:假,CN: "问题 2 - CN",FR:问题 2 -FR"}

如果需要,我可以发布我的代码,但是它很长而且很复杂.

解决方案

如果你创建了一个

直播 plunker 示例

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

this.questionForm = fb.group({
  questions: fb.array(this.questions.map(this.createQuestionControl(fb)))
});

createQuestionControl(fb: FormBuilder) {
  return (question, index) => {
    const checkbox = question.selected
    const answerbox = question.selected ? ['', [Validators.required, Validators.minLength(4)]] : ''
    return fb.group({question: checkbox, answer: answerbox, questionNumber: index + 1});
  }
}

changeValidator(selected, index) {
  const answerbox = this.questionForm.get('questions.' + index).get('answer')

  const validators = selected ? [Validators.required, Validators.minLength(4)] : null
  answerbox.setValidators(validators);
  answerbox.updateValueAndValidity();
}

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 answer

{ question: true, answer: ['', [Validators.required, Validators.minLength(4)]], index: 4 }

The changeValidator() method will add or remove validators on the answer if the question is changed (note: do not forget updateValueAndValidity)

template

<form [formGroup]="questionForm" (ngSubmit)="submit(questionForm)">

  <div formArrayName="questions">
    <div *ngFor="let question of questionForm.get('questions').controls | orderBySelected; let i = index;" [formGroupName]="i">
      <!--{{questionForm.get('questions.' + i + '.questionNumber').value}}-->
      {{questions[questionForm.get('questions.' + i + '.questionNumber').value - 1]['EN']}}
      <input type="checkbox" formControlName="question" (ngModelChange)="changeValidator($event, i)"/>
      <input type="text" formControlName="answer" />
      <em *ngIf="questionForm.get('questions.' + i + '.answer').invalid">Minimum length 4</em>
    </div>
  </div>

  <button type="submit" [disabled]="questionForm.invalid">Submit</button>

</form>

Following a clarification in the comments:

Maximum of 5 can be checked at a given time

I have updated the array to have cross field validation of no more than 3 (easier to test you can change it to 5)

export function max3Selected(formArray) {

  let totalSelected = formArray.controls.reduce((selectedControls, control) => 
  {
    if (control.get('question').value) {
      selectedControls++
    }
    return selectedControls;
  }, 0)

  return totalSelected > 3 ? { moreThanThreeSelected: true } : null;
}

and you would change the fb.array to include the validator function

fb.array(this.questions.map(this.createQuestionControl(fb)), max3Selected)

Screenshot of result

Live plunker example

这篇关于将复选框控件与文本框控件关联的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆