Validation does not occur once the data has been loaded

I developed a function that calculates the percentage of form completion.

However, I am encountering an issue where when editing an existing record, the validation does not kick in until data is edited in one of the inputs.

<div ng-form="dtform" class="container-fluid form-horizontal" ng-cloak>
    <fieldset ng-form="requestor_tab"> ... </fieldset>
    <fieldset ng-form="some_tab"> ... </fieldset>
</div>

The events triggering the function are:

$scope.$watch("data", () => { calculateCompleted(); }, true);
$scope.$watch("dtform.$valid", () => calculateCompleted());

Function for calculation:

let calculateCompleted = () => {
    let errors = 0; 
    let success = 0;
    settings.tabs.forEach(tab => {

        // The issue lies here: $scope.dtform[tab].$$success is undefined.
        if ($scope.dtform && $scope.dtform[tab] && $scope.dtform[tab].$$success && $scope.dtform[tab].$$success.required)
            success += $scope.dtform[tab].$$success.required.length;
    });
    // some mathematical calculations
    return percentage;
};

It seems that $scope.dtform[tab].$$success is undefined while running this function, although it has a defined value when checked in the console.

I suspect that there might be an additional trigger missing. Can anyone suggest what this trigger could be?

Thank you

Edit: I know a solution can be to trigger this function from the template

<div ng-hide="checkCompetion()"></div>
but I am looking for a more elegant resolution.

Answer №1

If there are no valid fields, then there may not be any defined tab objects.

You can try adding the following code:

if($scope.dtform.hasOwnProperty(tab)){ 

Modify it to this:

settings.tabs.forEach(tab => {

    // Try adding it here.
    if($scope.dtform.hasOwnProperty(tab)){


        if ($scope.dtform && $scope.dtform[tab] && $scope.dtform[tab].$$success && $scope.dtform[tab].$$success.required)
            success += $scope.dtform[tab].$$success.required.length;


    }

});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I prevent a button from being clicked in React JS if the input field is empty?

React Code Snippet: import { useState } from "react"; export default function IndexPage() { const [text, setText] = useState(""); const autoComplete = (e) => { setText({ value: e.target.value }); }; return ( <fo ...

Anticipate commitments during onbeforeunload

Is there a way to trigger a $http.get request when the page is closed? I encountered an issue where promises cannot be resolved because once the final method returns, the page is destroyed. The challenge lies in the fact that onbeforeunload does not wait ...

Exploring Selenium: Techniques for examining the source code of an unidentified function

I'm fairly new to using Selenium. I have come across this javascript code snippet and I am attempting to use Selenium to inspect the script, but I haven't had much luck so far. My main goal is to employ Selenium to access/inspect the script in or ...

When attempting to push `content[i]` into an array in AngularJS, it is flagged

In my JSON data, I have the following structure: var data = [{ id: 1, name: 'mobile', parentid: 0, limit:3 }, { id: 2, name: 'samsung', parentid: 1 }, { id: 3, name: 'moto', parenti ...

Utilizing Loops to Generate Unique CSS Designs on an HTML Page

View reference image ->Take a look at the reference image provided. ->In the image, a for loop is used to create box designs and displayed above. ->The goal is to change the background color and border color of all boxes using a single HTML cla ...

The automation script for Playwright/Puppeteer is having trouble properly handling `async...await` in a `for..loop` on the `/signup` page

Currently, I am faced with the challenge of automating rate-limit requests using a playwright automation script. The issue arises when the script keeps attempting to sign up with the email <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data ...

What are some strategies for improving search efficiency in arrays containing over 50,000 elements?

I am working with a large array of strings containing about 50,000 elements. export const companies = [ "000014", "000016", "000017", "000019", "000020", "000021", "000023" ...

Tips for updating the firebase access_token with the help of the next-auth credentials provider

Can anyone help me with refreshing the Firebase access token when it expires? I need the token for API authentication, but I can't find any information online regarding next-auth and Firebase. Currently, I am able to retrieve the access token but str ...

The XMLHttpRequest response states that the preflight request did not meet the access control check requirements

I am attempting to subscribe to a firebase cloud messaging topic by sending an http post request. var data = null; var xhr = new XMLHttpRequest(); xhr.withCredentials = true; xhr.addEventListener("readystatechange", function () { if (this.readyState ...

What is the process of handling XML responses using node.js and express?

My task is to handle this using node.js and express: <set id="1" state="0" name="wd"/> I attempted the following approach: xml = require('xml'); res.set('Content-Type', 'text/xml'); res.send(xml('<set id="1" ...

Is there a way to track dynamic changes in window dimensions within Vue?

Working on my Vue mobile web app, I encountered an issue with hiding the footer when the soft keyboard appears. I've created a function to determine the window height-to-width ratio... showFooter(){ return h / w > 1.2 || h > 560; } ...and ...

HighCharts velocity gauge inquiry

I recently integrated a highcharts speedometer with PHP and MYSQL on my website. Everything seemed to be working smoothly until I added the JavaScript code for the speedometer, causing it not to display. There are no error messages, just a blank screen whe ...

Building React applications with server-side rendering using custom HTML structures

I recently started using Suspense in my React app and decided to implement SSR. However, as I was going through the documentation at https://reactjs.org/docs/react-dom-server.html#rendertopipeablestream, I couldn't find a clear explanation on how to u ...

The Click Event is failing to trigger for a dynamically created ID within a span element

I am currently working on a Task Project. In this project, I have been adding items dynamically and generating items dynamically as well. However, when it comes to deleting items by clicking on a span element, the click event doesn't seem to work. I ...

Determine distinct items in an array that match a predefined criteria

I have a list of objects with two keys, img1 and img2. I need to identify unique objects based on the values of img1, while also retaining the corresponding value of img2. This is the current code I am using: const imgs_arr = [ ...new Set( inpu ...

Discovering the significance of a function's scope

I'm a bit confused about how the answer is coming out to be 15 in this scenario. I do understand that the function scope of doSomething involves calling doSomethingElse, but my calculation isn't leading me to the same result as 15. function doSo ...

A guide on breaking down a URL string containing parameters into an array with the help of JavaScript

I need help splitting a long string into an array with specific index structure like this: fname=bill&mname=&lname=jones&addr1=This%20House&... I am looking to have the array set up as shown below: myarray[0][0] = fname myarray[0][1] = b ...

The incorporation of zoom disrupts the smooth scrolling capability of the menu

My landing page has a menu that scrolls users to the selected section. However, my client prefers the page at a 90% zoom level. To accommodate this request, I added the following line of code: body { zoom:90%; } Unfortunately, when I click on a menu o ...

What is the ideal method for creating multi-view layouts in ASP.NET MVC?

I've been pondering the best approach to create multi-view layouts using ASP.NET MVC. For instance, in angular-based SPAs, loading different views into a layout is quite simple: <div ui-view="header"></div> <div ui-view="content">& ...

Using Jquery to retrieve the window size and dynamically apply it to a div's CSS, adjusting it accordingly when the

Hey there! I'm trying to create a full-screen div, but unfortunately CSS doesn't seem to do the trick in this case. JavaScript/jQuery is my only option. My plan is to dynamically set the size of the div based on the browser window's dimensi ...