AngularMaterial+Formly:ValidationinsideNestedCustomTypes
创始人
2024-10-28 07:01:41
0次

在使用 Angular Material 和 Formly 进行表单开发时,有时需要对嵌套的自定义类型进行验证。以下是一个示例,展示了如何在 Formly 模型中嵌套自定义类型并在 Angular Material 中添加验证。

首先,我们需要定义一个父级自定义类型,用于嵌套子级类型:

import { FormlyFieldConfig } from '@ngx-formly/core';

export interface NestedTypeModel {
  title: string;
  content: string;
  nested: NestedType;
}

export interface NestedType {
  title: string;
  content: string;
}

export class ParentType {
  static fieldGroup: FormlyFieldConfig[] = [
    {
      key: 'title',
      type: 'input',
      templateOptions: {
        label: 'Title',
        required: true,
      },
    },
    {
      key: 'content',
      type: 'textarea',
      templateOptions: {
        label: 'Content',
        required: true,
      },
    },
    {
      key: 'nested',
      type: 'custom',
      templateOptions: {
        label: 'Nested Type',
      },
      validators: {
        nested: {expression: 'formState.valid', message: 'Nested type is invalid'},
      },
      fieldGroup: [
        {
          key: 'title',
          type: 'input',
          templateOptions: {
            label: 'Title',
            required: true,
          },
        },
        {
          key: 'content',
          type: 'textarea',
          templateOptions: {
            label: 'Content',
            required: true,
          },
        },
      ],
    },
  ];
}

注意,在父级类型的 “validators” 对象中添加了一个自定义验证器“nested”,用于验证嵌套的子级类型是否有效。

接下来,我们需要定义一个自定义类型,该类型用于嵌套在父级类型中:

import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig, FormlyFormOptions } from '@ngx-formly/core';
import { NestedTypeModel } from './parent-type.model';

export class NestedTypeComponent {
  title;
  content;
  form = new FormGroup({});
  options: FormlyFormOptions = {};
  fields: FormlyFieldConfig[] = [
    {
      key: 'title',
      type: 'input',
      templateOptions: {
        label: 'Title',
        required: true,
      },
      validators: {
        validTitle: {expression: '!!model.title && model.title.length > 5', message: 'Title must be at least 5 characters'},
      },
    },
    {
      key: 'content',
      type: 'textarea',
      templateOptions: {
        label: 'Content',
        required: true,
      },
      validators: {
        validContent: {expression: '!!model.content && model.content.length > 10', message: 'Content must be at least 10 characters'},
      },
    },
  ];

  constructor() {}

  get model(): NestedTypeModel {
    return {title: this.title, content: this.content, nested: null};
  }

  get formState(): any {
    return {valid: this.form.valid};
  }
}

在该自定义类型中,我们添加了一个表单分组和一些子级字段。还添加了一些自定义验证器来验证子级类型的特定字段。

最后,我们可以将父级类型和子级类型组合在一起,并将其添加到 Formly 的配置中:

相关内容

热门资讯

WPK透视版透视挂练习!wpk... WPK透视版透视挂练习!wpk辅助购买,德扑之星ai多功能透视挂工具1)wpk辅助购买辅助插件:进一...
WPK安装透视资料!wepok... WPK安装透视资料!wepoker游戏下载,德州扑克ai多功能透视appwepoker游戏下载是不是...
WPK真假透视方式!wepok... WPK真假透视方式!wepoker辅助透视软件,wpkai多功能透视技巧1、wepoker辅助透视软...
WPK插件透视挂方针!wepo... WPK插件透视挂方针!wepoker有机器人吗,wpkai多功能透视挂脚本1、进入游戏-大厅左侧-新...
WPK多开辅助器总结!德普软件... WPK多开辅助器总结!德普软件,微扑克ai多功能辅助器神器1、每一步都需要思考,不同水平的挑战德普软...
WPK购买辅助器要领!wepo... WPK购买辅助器要领!wepoker代打辅助机器人,aapokerai多功能辅助器技巧1、起透看视 ...
WPK四人透视大纲!wepok... WPK四人透视大纲!wepoker免费脚本咨询,wpkai多功能透视插件1、完成wepoker免费脚...
WPK俱乐部辅助器讲义!wep... WPK俱乐部辅助器讲义!wepoker有透视底牌吗,德州扑克ai多功能辅助器脚本1、操作简单,无需w...
WPK模拟器透视妙招!wpk透... WPK模拟器透视妙招!wpk透视辅助下载,AApokerai多功能透视工具小薇(辅助器软件下载)致您...
WPK真假开挂要领!wpk软件... WPK真假开挂要领!wpk软件是正规的吗,红龙PoKerai多功能开挂神器1、任何wpk软件是正规的...