The validation directive is run on each individual item within the ng-repeat loop

As I develop a single page application utilizing Angular and Breeze, the challenge of managing entities with dynamic validation arises. With a set of entities displayed on the page using data-ng-repeat, I implement in place validation through toggling between "edit" and "display" modes by controlling visibility using data-ng-hide and data-ng-show directives. However, only one entity can be in edit mode at a time.

In the edit mode div, multiple inputs are bound to the Breeze entity via data-ng-model, and the Breeze entity validation is integrated using data-z-validate based on the directive from Breeze labs documentation (http://www.breezejs.com/breeze-labs/breezedirectivesvalidationjs).

Simplified HTML structure:

<div data-ng-controller="payees as vm">
    <div data-ng-repeat="payee in vm.payees">
        <div data-ng-hide="payee.editing">
            <span>{{payee.name}}</span>
            <span>{{payee.addressLine1}}</span>
            <span>{{payee.town}}</span>
            <span>{{payee.postcode}}</span>
        </div>
        <div data-ng-show="payee.editing">
            <input data-z-validate data-ng-model="payee.name" />
            <input data-z-validate data-ng-model="payee.addressLine1" />
            <input data-z-validate data-ng-model="payee.town" />
            <input data-z-validate data-ng-model="payee.postcode" />
        </div>
    </div>
</div>

The issue arises when every input triggers the validation directive code for all items in the repeater, even those that are hidden. This leads to a performance concern especially with numerous entities as each input validation runs multiple times unnecessarily.

Seeking a solution to ensure validation occurs solely for the entity in edit mode or visible inputs only, optimizing performance. Any suggestions to accomplish this selective validation approach?

Answer №1

When dealing with mutually exclusive conditions, such as a ngShow and a ngHide with the same value, it is advisable to utilize the ngSwitch directive, as suggested in the AngularJS FAQ:

Pay special attention to the robustness of ng-switch which should be preferred over multiple mutually exclusive ng-show.

In this scenario, employing ngSwitch while in "display mode" will eliminate the existence of <input> tags and prevent the execution of validation directives.

<div data-ng-controller="payees as vm">
    <div data-ng-repeat="payee in vm.payees" data-ng-switch="payee.editing">
        <!--Content for display mode-->
        <div data-ng-switch-when="false">
            <span>{{payee.name}}</span>
            <span>{{payee.addressLine1}}</span>
            <span>{{payee.town}}</span>
            <span>{{payee.postcode}}</span>
            <!--Add more markup here to switch to edit mode-->
        </div>
        <!--Content for edit mode-->
        <div data-ng-switch-default>
            <input data-z-validate data-ng-model="payee.name" />
            <input data-z-validate data-ng-model="payee.addressLine1" />
            <input data-z-validate data-ng-model="payee.town" />
            <input data-z-validate data-ng-model="payee.postcode" />
            <!--Add more markup here to save changes or cancel edit mode-->
        </div>
    </div>
</div>

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

Retrieve the specific day of the previous complete week within a designated month using JavaScript

Is there a way to determine the last full week of any given month? For example, I need to find the Wednesday of the last full week in April each year. Here is the desired output: April 24, 2019, April 22, 2020, April 28, 2021, April 27, 2022, and so on. v ...

Steer clear of 405 errors by implementing AJAX in combination with Flask and JINJA templ

Hey there, I'm fairly new to backend work so please bear with me. I've been doing some research but haven't found the answer yet. Currently, I'm working on an application that fetches search results from a 3rd party API. I'm tryi ...

Set the value of a variable to the result of a JavaScript function

I recently wrote a function that retrieves JSON data from a specified URL. Here's what the function looks like: function getJSON(url) { request.get({ url: url, json: true, headers: { 'User-Agent': 'request&a ...

suggestions for customizing Angular Material

The guidelines regarding materials specify that: "For any Angular Material component, you are allowed to define custom CSS for the component's host element that impacts its positioning or layout, such as margin, position, top, left, transform, and z- ...

The error message "Uncaught ReferenceError: exports is not defined and require" indicates that

I am currently developing an app using angularjs and typescript, but I've encountered a persistent error that has me stumped. Below is the snippet of my code: export var NgApp = new application.Startup(); ///<reference path="../../../../../typin ...

Unable to conduct a directive scope test using Mocha

Trying to test a simple directive with an isolated scope, but facing issues with testing the scope variables defined in the link function. Here is a simplified version of the directive and spec: Directive: angular.module('myApp').directive(&a ...

Synchronizing timers among different elements

Just a little context: I'm diving into the world of React and currently working on a small app using next.js (with a template from a tutorial I followed some time ago). Lately, I've encountered a challenge where I need to synchronize a timer betw ...

When the drawer is opened, there is a strange phenomenon of all buttons being mysteriously clicked

Currently, I am working on a single-page web application utilizing React and Material UI, along with React-Mini-Router for routing. The app features a side drawer that is activated by clicking a hamburger icon located in the top app bar. Each item in the d ...

"Encountering issue with componentDidMount() not functioning as expected, despite debugger being

Hello everyone! This is my first time sharing something here, so I'm eager to hear your feedback on both how I've posted and the content itself. I've only been programming for 8 weeks now, so I'm sure there are plenty of mistakes in wha ...

Mastering the Art of Parsing Complex JSON Data

I received a JSON output that looks like this. Using getjson, I'm trying to extract the datetime and value fields (italicized and bolded) such as [16:35:08,30.579 kbit/s],[16:35:38,23.345 kbit/s]. Is there any solution to achieve this? Thank you. { ...

Can you explain the process of extracting images from JSON data using AJAX and jQuery?

Hello, I'm looking for guidance on incorporating jquery with AJAX to fetch images from a JSON file and showcase them on my website. Below is the code snippet I have put together. function bookSearch(){ var search = document.getElementById('sea ...

What steps should I follow to properly set up my tsconfig.json in order to ensure that only the essential files are included when executing npm run build

Introduction I am seeking guidance on how to correctly set up my tsconfig.json file to ensure only the necessary files are included when running npm run build for my projects. I want to avoid any unnecessary files being imported. Query What steps should ...

When the submit button is clicked on a React form, it not only submits the current form

I am working on a React application with multiple forms, where each form is rendered based on the current page index. I would like the "Next" button that retrieves the next component to also act as a submit button. The issue I am facing is that while the n ...

The sessionToken is invalidated upon the user updating their password

My mobile hybrid app is connected to an expressJS server that acts as the backend for proxying requests to parse.com through the REST API. Additionally, I have implemented user authentication using express with a Single Sign-On (SSO) provider. Although I f ...

Struggling to get Canvas2ImagePlugin to function properly within an Ionic app and Angular.js

Recently, I stumbled upon a plugin that allows me to save images in my App, a solution I have been struggling to find. My question is: how do I integrate this into an angular framework? As a newcomer to angular.js, I'm feeling quite lost. The code sni ...

Dispatching identification to controller after submission

I am encountering an issue with a page that contains the following code: <div class="col-sm-2 displaybutton"> <div data-point-to="displaysHeader"> @using (Html.BeginForm("Administration", "Home", FormMethod.Post)) ...

"Performing a MongoDB Node query within the time frame of 1 hour from

I am having trouble retrieving data with my query to find all objects within the last hour based on timestamps: Here is the schema I am using: var visitSchema = mongoose.Schema({ timestamp: { type: Date, default: Date.now }, userID: String, userName ...

Issue: Module '/Users/MYNAME/Desktop/Projects/MYPROJECTNAME' not found

I am currently in the process of compiling Node using TypeScript, and I'm still getting acquainted with it. An issue I encountered was that my /src files were not being updated when I made changes and restarted the server. To troubleshoot, I decided ...

Error: The attempt to access the 'useContext' property of null has failed due to a TypeError

Nowhere in my React code am I using the useContext property. There is a compiled webpack file in an npm package with a component inside. When trying to use this component in my React app, it throws an error: Uncaught TypeError: Cannot read properties of nu ...

Efficiently adding values to a variable with the forEach method

I'm encountering an issue with displaying data from a JSON file similar to the one below: Currently, "checked" is set to null. I aim to update it to true within the steps array using a forEach loop. JSON data snippet: { "id": 4, "process": { ...