【问题标题】:Set custom error message in minLength validator in ngx-formly在 ngx-formly 的 minLength 验证器中设置自定义错误消息
【发布时间】:2019-07-03 15:27:49
【问题描述】:

我想创建一个带有验证的简单表单,并且我想将所有错误消息翻译成西班牙语。它适用于 'required'、'min' 和 'max' 验证器,但是当我对 'minLength' 和 'maxLength' 执行相同操作时,不会显示错误消息。

这很奇怪,因为这些字段仍然被标记为不正确(它们显示为红色),但没有出现错误消息。只是没有调用消息格式化函数。

这是我的代码的基本部分:

    form = new FormGroup({});
    model = { email: 'email@gmail.com' };

    validation = {
        messages: {
            required: (error, field: FormlyFieldConfig) => `Este campo es obligatorio.`,
            min: (error, field: FormlyFieldConfig) => `El valor debe ser mayor o igual que ${error.min}.`,
            max: (error, field: FormlyFieldConfig) => `El valor debe ser menor o igual que ${error.max}.`,
            minLength: (error, field: FormlyFieldConfig) => `La longitud mínima es ${error.minLength}.`,
            maxLength: (error, field: FormlyFieldConfig) => `La longitud máxima es ${error.maxLength}.`
        }
    };

    fields: FormlyFieldConfig[] = [ {
        fieldGroupClassName: 'row',
        fieldGroup: [
            {
                key: 'reference',
                type: 'input',
                className: 'col-xs-6',
                templateOptions: {
                    label: 'Referencia',
                    type: 'string',
                    required: true,
                    minLength: 3,
                    maxLength: 30,
                },
                validation: this.validation
            } ]
    }];

【问题讨论】:

    标签: angular ngx-formly


    【解决方案1】:

    有点晚了,但验证应该是:

    minlength: (error, field: FormlyFieldConfig) => `La longitud mínima es ${error.requiredLength}.`
    

    验证名称中的基本小写“l”和使用的错误属性“requiredLength”。在 ngx-formly 版本 5.5.2 上对其进行了测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-05
      • 2011-10-12
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      相关资源
      最近更新 更多