Verifying the name's uniqueness and disregarding it in case of any alterations

I am currently working on a form that includes a modal to allow users to modify their name and email address. The issue I am facing is that the form sends a request to the server to check for unique values in all cases. While this is appropriate when creating a new record, it becomes problematic when modifying existing information because I want to ignore the current value.

<form>
   <input type="text" ng-model="username" unique-url="/test/to/unique" />
</form>

To load this modal form, I change the model value like so:

$scope.showModal = function(row) {
    $scope.username = row.name;
    showTheFormModal();
}

Below is the directive used:

myAPP.directive('uniqueUrl', ['$http', function($http) {
    return {
        require: 'ngModel',
        link: function(scope, ele, attrs, c) {

            var url = attrs.uniqueNameUrl + '?';

            scope.$watch(attrs.ngModel, function() {

                $http({
                    method: 'GET',
                    url: url + $.param({'name': ele.val()})
                }).success(function(isUnique,status,headers,cfg) {
                        var iu = isUnique == 'true' ? true : false;
                        c.$setValidity('unique', iu);
                    }).error(function(data,status,headers,cfg) {
                        c.$setValidity('unique', false);
                    });
            });
        }
    }
}])

Answer №1

Consider performing the uniqueness check on the server side rather than on the client side. Send the database identifier back from the server and save it along with the username and password. The client can then verify the id value before sending it in future requests. Implement an additional $http put route for updating existing entries in the database.

$http.put('/update/' + id, UpdatedData)

$http.get('/data/')

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

Issues with expanding all nodes in the Angular Treeview function by Nick Perkins in London are causing difficulties

Currently utilizing the angular treeview project found here: https://github.com/nickperkinslondon/angular-bootstrap-nav-tree After examining the functionality, it seems that this treeview is lacking search capabilities. To address this limitation, I deci ...

Switching between fixed and unfixed divs causes two absolute divs to alternate

I am currently working on a code to keep a fixed div ("two") in place between two absolute positioned divs ("one" and "footer") while scrolling. However, there is an issue that arises when the browser window is resized. The distance between the footer and ...

What is the most effective method to prevent the auto-complete drop-down from repeating the same value multiple times?

My search form utilizes AJAX to query the database for similar results as the user types, displaying them in a datalist. However, I encountered an issue where it would keep appending matches that had already been found. For example: User types: "d" Datali ...

Ways to showcase multiple elements using introjs

I am attempting to highlight multiple DOM elements using the JS library intro.js, but I am encountering an issue. It seems that I can only define one element to be highlighted at a time. introjs.setOptions({ steps: [ { ...

I recently created an application using Vue and I'm exploring options for storing information to use later. Would using js-cookie be a viable solution for this?

After experimenting with Vuex to store user-selected information in my app, I found that the data was lost upon page reload. Switching to Cookies and using js-cookie solved this issue seamlessly. It makes me wonder if there is a downside to relying on cook ...

Angular JS modal form popup is a sleek and user-friendly feature that

When trying to display a modal form popup, I encountered an issue where it was not appearing. Upon inspecting the element, I received an error stating that $(...)modal is not a function. AuthService.createAdmin(data, function (response) { ...

Anticipate the definition of a $scope variable from the parent controller

I have a scenario in my AngularJS application where an asynchronous $http request is used to load data in the parent controller, and then I need to wait for this data to be loaded before making another $http request in a child controller to fetch more deta ...

The Req.session array is limited to storing just one element at a time

I'm currently working on integrating a shopping cart feature into my Express/MongoDB e-commerce app that sells sneakers. To add an item to the cart, I extract the quantity and size from req.body and the itemId from req.session (previously saved when l ...

Tips for sending a PDF created with jsPDF as an attachment in an email using asp.net c#

I'm wondering if there's a way to attach a PDF file that was generated using jsPDF and email it in asp.net C#? This is the code snippet I have in c#: MailMessage message = new MailMessage(fromAddress, toAddress); message.Subject = subj ...

What is an alternative way to rewrite this regular expression without relying on the deprecated API?

My JavaScript code uses a regular expression, myRegexp, to match numbers in a string: var myRegexp = new RegExp('[0-9]+'); The code then extracts numbers from the string and returns an array: var string = '123:456'; var nums = []; wh ...

Is there a Joomla extension available that can display or conceal content upon clicking?

Looking to enhance my Joomla site by installing a plugin that allows me to toggle the visibility of content with a click, similar to how FAQ sections work on many websites. Question 1 (click here for the answer) { Details for question 1 go here } Questi ...

Using jQuery to Validate Input Text Control Depending on Radio Selection

How can I ensure that the input text linked to the selected radio option is filled in? For instance, in the example above: If Contact 1's Email radio option is chosen, the Email text field for Contact 1 must not be empty, while the Phone and US Mai ...

Error: The 'filename' property of undefined cannot be read when attempting to upload a user profile photo using multer

I am facing an issue while attempting to upload a user profile photo using express.js server and Multer. I keep receiving the error message "TypeError: Cannot read property 'filename' of undefined." Below is the code snippets for both the server- ...

Utilize Jquery and MVC 5 to create a reoccurring partial view

I have a button that triggers the loading of a partial view containing a table with all the categories in my system. <input type="button" value="Load Categories" id="btnLoadCategories" /> This loading process is done via a partial view which is in ...

Function exhibiting varying behavior based on the form from which it is called

Currently, I'm utilizing AJAX to execute a server call, and I've noticed that the behavior of my function varies depending on its origin. Below is an excerpt of the code that's causing confusion: <html> <body> <script> ...

Using Ajax script to transfer user information to a php file in order to refresh the modified row in a table

Recently, I created a table in PHP to display certain data. Here's how it looks: <?php echo '<table id="tableteste" class="table table-striped" width="100%">'; echo '<thead><tr>'; echo &apos ...

Revise the display to show the formatted value, while adjusting the model to reflect the

How can I update the model value to be "valueToFormat" after formatting input boxes based on the user's locale and storing the value in en-US locale on blur? I am able to format the fields correctly for the user to view, but struggling with updating t ...

The error "Unable to create an instance of mssql.Schema" indicates that the

Seeking assistance from experienced ReactJS developers to address the issue outlined below. The code provided is based on a tutorial I was following. Despite numerous attempts, I have been unable to resolve the issue. Here is the code snippet from User.js ...

Problem with geocoding to retrieve a list of terrestrial coordinates

I am currently working on developing a functionality that can return an array of land-based coordinates. I have utilized Google's geocoder to create individual land-based coordinates successfully, but now I want to implement it in a way where an array ...

What causes the left click to not trigger in Kendo's Angular Charts?

My homepage features a simple bar chart that displays correctly, but I am having trouble capturing the left click event (the right click works fine). This is an example of code from my template: <kendo-chart *ngIf="(dataExists | async)" [ ...