Formly Validate angularjs 字段不一样

Validate formly angularjs fields are not the same

我在点击时动态添加了字段。

       addNewFiled() {
        let parent = this;
        this.scope.fields.push({

            key: 'field-'+parent.scope.fields.length,
            type: 'horizontalInput',
            templateOptions: {
                placeholder :'Enter Field',
                label: 'Filed',
                required: false
            },
            validators: {
                fieldFormat: function($viewValue, $modelValue, scope) {

                    let value = $viewValue;
                    if(value.length != 12){
                        scope.to.message = "Field should be 12 characters";
                        return false;
                    }

                    return true;
                }
            }

        });


    }

我需要验证输入的值不在验证器的另一个字段中,我尝试循环遍历模型但效率不高,感谢任何帮助。

遇到过一次,用2张图解决了 基本上,您将有 2 个映射,一个将包含映射到它的值的字段索引,第二个映射将包含映射到该值重复次数的字段值 在您的验证器中,您减少先前值的重复次数(在完成其他验证后)并增加新值的重复次数并检查它是否大于 1 然后重复。

在你的对话框中定义两个地图

    private valuesMap: any = [];
    private keysArray:any = [];

在你的字段中,你注入了一个控制器来保存当前字段的索引

     controller: function ($scope) {
                $scope.index = parent.scope.fields.length-1;
                parent.keysArray[$scope.index] = $scope.index;
            },

然后在你的验证器中

    if(value) {


        if(angular.isDefined(parent.valuesMap[parent.keysArray[scope.index]])) {
            parent.valuesMap[parent.keysArray[scope.index]]= parent.valuesMap[parent.keysArray[scope.index]] -1;

        }

        parent.keysArray[scope.index] = value;
        if(angular.isDefined(parent.valuesMap[value]) && parent.valuesMap[value] > 0) {
            parent.valuesMap[value] = parent.valuesMap[value]+1;
            scope.to.message = "Value is already entered";
            return false;
        }
        parent.valuesMap[value] = 1;


    }

希望这适用于您的场景

您不需要对此进行验证,通过 templateOptions 中的 minlength 和 maxlength 属性已经对字段长度进行了默认验证。

只需这样做:

templateOptions: {
                placeholder :'Enter Field',
                label: 'Filed',
                required: false,
                minlength: 12,
                maxlength: 12
            },