0

I want to create a simple form with validations, and I want to translate all error messages into Spanish. It works fine for 'required', 'min' and 'max' validators, but when I do the same with 'minLength' and 'maxLength', no error message shows.

It is weird because the fields are still marked as incorrect (they appear in red), but no error message appears. The message formatting functions are just not called.

Here is the essential part of my code:

    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
            } ]
    }];

4

1 回答 1

1

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

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

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

于 2019-11-08T09:57:01.983 回答