简体   繁体   English

Angular Schema Form-数组验证

[英]Angular Schema Form - Array validation

I am trying to make an array of unique values using Schema Form but it seems like an impossible task. 我正在尝试使用Schema Form创建一个唯一值数组,但这似乎是不可能的任务。

To keep things simple lets consider this simple unique validator: 为了简单起见,请考虑以下简单的唯一验证器:

$scope.validateUnique = function(value) {
    console.log('running validation');
    var sameName = $scope.model.NoDuplicate.filter(function(item) {
        return item.key === value;
    });
    return sameName.length < 2;
}

Open your console and visit this Fiddle . 打开控制台并访问此Fiddle

Step 1: Add new item called 1. 步骤1:添加名为1的新项目。

Step 2: Add new item called 12. 步骤2:添加名为12的新项目。

Step 3: Add new item called 123. 步骤3:添加名为123的新项目。

Step 4: Add new item called 1234. 步骤4:添加名为1234的新项目。

So far so good. 到现在为止还挺好。

Step 5: Change the first item (1) to 1234 so that an error message appears. 步骤5:将第一个项目(1)更改为1234,以便出现错误消息。

Step 6: Delete the last item so that the first one is unique again. 步骤6:删除最后一个项目,以便第一个再次具有唯一性。 But nothing happens. 但是什么也没发生。 The array is not validated again once an item is removed and even if you click on the submit button which should trigger the valdiation of form nothing happens. 一旦删除了一个项目,即使您单击提交按钮(该按钮应该触发表单的验证),数组也不会再次验证,什么也不会发生。

Is this issue my mistake? 这是我的错误吗? Is there some mistake in my example code? 我的示例代码中有什么错误吗? Or is it some sort of Schema-Form bug? 还是某种架构错误? How could I achieve revalidation of the array either on removing of item or submitting the form? 如何在删除项目或提交表单时实现数组的重新验证? Thanks! 谢谢!

Solved. 解决了。 I am not proud of the solution as far as the Angular Schema Form array validation is broken I couldn't figure out better solution. 就Angular Schema Form数组验证被破坏而言,我不为该解决方案感到骄傲,我无法找到更好的解决方案。

I have added this to the form directive 我已将此添加到form指令

$scope.$watch('formState', () => {
    TestStartActions.updateFormStateFromForm(newVal);
    angular.forEach($scope.testStartForm.$error.runtimeUnique, (field) => {
        field.$validate();
    });
}, true);

Here is working fiddle . 这是工作的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM