Angular-Formly: сделать поле обязательным, если опция выбрана из выпадающего списка - PullRequest
0 голосов
/ 22 мая 2018

Я хочу сделать поле ввода обязательным, если в соответствующем раскрывающемся списке была выбрана опция.Мои 2 поля дублируются после выбора опции из выпадающего списка, чтобы пользователь не ограничивался одной опцией.Итак ... если выбрано exportSupplier, то agreementReference становится required.Однако, если существует более одного exportSupplier и хотя бы одно из них не выбрано, то форма обрабатывает поля agreementReference как not required.

Как создать соответствующее поле agreementReference requiredесли был выбран соответствующий exportSupplier?

    vm.exportSuppliers = [{ exportSupplier: '', agreementReference: '' }];

    vm.exportSupplierFields = [
        {
            fieldGroup: [
                {
                    className: 'col-xs-6',
                    key: 'exportSupplier',
                    type: 'select2',
                    templateOptions: {
                        label: 'Export Supplier',
                        required: false,
                        options: [],
                        onChange: function() {
                            vm.addExportSupplier();
                        }
                    }
                },
                {
                    className: 'col-xs-5',
                    key: 'agreementReference',
                    type: 'input',
                    templateOptions: {
                        label: 'Agreement Reference',
                        onChange: function() {
                            vm.addExportSupplier();
                        }
                    },
                    expressionProperties: {
                        'templateOptions.required': '!!model["exportSupplier"]'
                    }
                }
            ]
        }
    ];

    vm.addExportSupplier = function() {
        if (vm.exportSuppliers[vm.exportSuppliers.length - 1].exportSupplier || vm.exportSuppliers[vm.exportSuppliers.length - 1].agreementReference) {
            vm.exportSuppliers.push({ exportSupplier: '', agreementReference: '' });
        }
    };

мой HTML

<div ng-repeat="supplier in vm.exportSuppliers" class="row">
    <formly-form model="supplier" fields="vm.exportSupplierFields"
        form="vm.informationExportSupplier.form" index="$index">
    </formly-form>
</div>

Я пробовал разные варианты, чтобы добавить $ index в это поле 'templateOptions.required', но безуспешно.

1 Ответ

0 голосов
/ 22 мая 2018

Я пробовал разные варианты, чтобы добавить $ index в это поле 'templateOptions.required', но безуспешно.

<div ng-repeat="supplier in vm.exportSuppliers" ng-init="outsideIndex = $index">
    <formly-form model="supplier" fields="vm.exportSupplierFields"
        form="vm.informationExportSupplier.form"  ̶i̶n̶d̶e̶x̶"̶$̶i̶n̶d̶e̶x̶"̶
        options="{data: {outsideIndex: outsideIndex}}">
    </formly-form>
</div>

Из документов:

options

Параметры формы.

data

Это объект.Положите сюда все, что вы хотите.

- Директива API в форме прямоугольной формы

...