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 的配置中:

相关内容

热门资讯

一分钟了解“微信微乐游戏辅助脚... 一分钟了解“微信微乐游戏辅助脚本”菠萝德州透视脚本(总是有挂);亲真的是有正版授权,小编(透视辅助软...
今日科普“乐酷解码辅助”xpo... 今日科普“乐酷解码辅助”xpoker怎么作弊(确实存在有挂);最新版2024是一款经典耐玩的益智游戏...
专业讨论“九酷众娱辅助”wep... 专业讨论“九酷众娱辅助”wepoker看底牌(一向真的是有挂)准备好在九酷众娱辅助ia的高塔上攀登,...
总算了解“随意玩辅助软件”po... 总算了解“随意玩辅助软件”pokemmo内置修改器(从来是真的有挂);原来确实真的有挂(需添加指定薇...
今日公布“琼崖辅助软件”德普之... 今日公布“琼崖辅助软件”德普之星透视辅助软件下载(确实有挂);支持多人共享记分板与复盘,通过邀请好友...
玩家必看教程“新道游辅助器透视... 玩家必看教程“新道游辅助器透视挂”德普之星透视辅助软件激活码(总是存在有挂);1分钟了解详细教程(微...
分享认知“广东雀神智能插件需要... 分享认知“广东雀神智能插件需要付费吗”wpk透视辅助(其实是真的有挂);实战中需综合运用上述技巧,并...
玩家必看科普“小程序卡五星辅助... 玩家必看科普“小程序卡五星辅助”wepoker私人局辅助器怎么用(其实真的是有挂);大神普及一款德州...
今日百科“指尖四川辅助脚本视频... 今日百科“指尖四川辅助脚本视频”wpk透视辅助下载(最初有挂);支持2-10人实时对战,虚拟庄家发牌...
我来教教你“道游互娱透视辅助截... 《我来教教你“道游互娱透视辅助截图”wepoker好友助力码(原本真的是有挂)》 道游互娱透视辅助截...