Angular Schema Form - Array validation

651 Views Asked by At

I am trying to make an array of unique values using Schema Form but it seems like an impossible task.

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.

Step 1: Add new item called 1.

Step 2: Add new item called 12.

Step 3: Add new item called 123.

Step 4: Add new item called 1234.

So far so good.

Step 5: Change the first item (1) to 1234 so that an error message appears.

Step 6: Delete the last item so that the first one is unique again. 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!

1

There are 1 best solutions below

0
On BEST ANSWER

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.

I have added this to the form directive

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

Here is working fiddle.