I am currently trying to dynamically validate JSON in AngularJS. Unfortunately, I have encountered an issue with loading fields from the schema onto the page. My understanding of AngularJS is limited as I am still new to it. Although I have managed to create the form, I am struggling to implement dynamic validation.
myApp.controller('appController', ['$scope','$http', function($scope, $http) {
var NUMBER_REGEXP = /^[0-9]+$/;
$http({ method: 'GET', url: 'JsonSchema.json' })
.then(function successCallback(response) {
$scope.data = angular.fromJson(response);
$scope.data = $scope.data.data;
console.log($scope.data);
});
$scope.greeting = 'success!';
$scope.handleErrors = function (data) {
if(data !== undefined && data !== null) {
angular.forEach(data, function(value, key) {
var form = value.form.field;
var fields = value.fields.field;
var error = form.errors;
if(form.compulsory) {
$scope.pageError = form.errors.compulsory;
}
else if(!(NUMBER_REGEXP.test(filds.field))) {
$scope.pageError = error.regex;
}
else {
$scope.pageError = "An unexpected error has occurred, please try again later!";
}
});
}
}
}]);