如何在Angular 5应用程序中为formGroup配置多个验证

时间:2018-05-16 16:13:26

标签: angular angular-reactive-forms

我正在进行Angular反应表单验证。我目前的结构只有validation.required,我需要额外的验证来设置,但努力这样做。 我需要验证    - minLength,    - 电子邮件和    - 邮政编码

我正在从json数据动态创建表单。

formGroup验证服务

@Injectable()
export class QuestionControlService {
constructor() { }

toFormGroup(questions: QuestionBase<any>[] ) {
let group: any = {};

questions.forEach(question => {
  group[question.key] = question.required ? new FormControl(question.value || '', Validators.required)
                                          : new FormControl(question.value || '');
   // need help here for forms.
  });
    return new FormGroup(group);
 }
}

基本问题组件

export class QuestionBase<T>{

key: string;
label: string;
required: boolean;
minLength:number;
order: number;
controlType: string;

constructor(options: {

    key?: string,
    label?: string,
    required?: boolean,
    minLength?:number,
    order?: number,
    controlType?: string,
  } = {}) {

  this.key = options.key || '';
  this.label = options.label || '';
  this.required = !!options.required;
  this.minLength = options.minLength;
  this.order = options.order === undefined ? 1 : options.order;
  this.controlType = options.controlType || '';
  }
 }

我还需要根据其类型自定义验证错误,目前仅供参考!如下面的代码

模板

<div [ngSwitch]="question.controlType">

       <div *ngSwitchCase="'textbox'"> <small>textbox</small>
           <div class="form-group">
               <input *ngSwitchCase="'textbox'" 
                      [formControlName]="question.key" 
                      [id]="question.key" 
                      [type]="question.type"
                      [(ngModel)]="question.value" 
                      (keyup.enter)="onChangeValue($event, previousValue, responseId, question.key, 'textbox'); previousValue = question.value" 
                      (blur)="onChangeValue($event,  previousValue, responseId, question.key, 'textbox'); previousValue = question.value"
                      (focus)="previousValue=question.value"
                      >
                      <span></span>
           </div>
       </div>
  <div class="errorMessage" *ngIf="!isValid">{{question.label}} is required</div> 
 </div>

2 个答案:

答案 0 :(得分:2)

FormControl接受单个验证器或验证器数组(ValidatorFn | ValidatorFn[])。您可以将多个验证器作为数组传递。

class FormControl extends AbstractControl {
  constructor(formState: any = null, 
              validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, 
              asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null)
new FormControl(question.value || '', [Validators.required, Validators.minLength(5)])

以下是显示多条错误消息的example code from angular docs

<input id="name" class="form-control"
       formControlName="name" required >

<div *ngIf="name.invalid && (name.dirty || name.touched)"
     class="alert alert-danger">

  <div *ngIf="name.errors.required">
    Name is required.
  </div>
  <div *ngIf="name.errors.minlength">
    Name must be at least 4 characters long.
  </div>
  <div *ngIf="name.errors.forbiddenName">
    Name cannot be Bob.
  </div>
</div>

答案 1 :(得分:0)

这是我修改的方式

@Injectable()
export class QuestionControlService {
constructor() { }

toFormGroup(questions: QuestionBase<any>[] ) {
   let group: any = {};

questions.forEach(question => {

  var validations: ValidatorFn[] = [];

  if(question.required) {
    validations.push(Validators.required)
  }

  if(question.minLength)
  {
    validations.push(Validators.minLength(question.minLength))
  }

  group[question.key] = new FormControl(question.value || '', validations);                                                                                    
 });
   return new FormGroup(group);
 }
}

将json数据映射到Text对象

 if(questionElementType=="textbox")
 {       

   let _textBox = new TextboxQuestion({
     consultationId: questionsList[key].consultationId,
     questionId: questionsList[key].questionId,
     questionElementType: questionsList[key].questionElementType[0].title,        
     questionType: questionsList[key].questionType,
     title:questionsList[key].title,
     displayId: questionsList[key].displayId,
     key: questionsList[key].questionId, 
     label: questionsList[key].title,
     value: questionsList[key].answer.length<=0? null : questionsList[key].answer[0].answerValue,
     required: true,
     minLength:8,
     order: 5
    }); 

    this.mappedQuestions.push(_textBox);

 }