Strategies for enabling form resubmission in AngularJS when encountering a Sequelize validation error

Having issues with the form.$valid property in my SignUp controller for user registration. It seems to be stuck at false when a validation error is thrown by Sequelize. Any suggestions on what I should modify in my controller? Perhaps resetting the $valid value for a specific input on an ng-change event could help, but I'm not sure how to implement this...

class SignupController {
  //start-non-standard
  user = {};
  errors = {};
  submitted = false;
  //end-non-standard

  constructor(Auth, $state) {
    this.Auth = Auth;
    this.$state = $state;
  }

  register(form) {
    this.submitted = true;
    console.log(this.errors);
    if (form.$valid) {
      console.log("click");
      this.Auth.createUser({
        nickname: this.user.nickname,
        email: this.user.email,
        password: this.user.password
      })
      .then(() => {
        // Account created, redirect to home
        this.$state.go('main');
      })
      .catch(err => {
        err = err.data;
        this.errors = {};

        // Update validity of form fields that match the sequelize errors
        if (err.name) {
          angular.forEach(err.fields, function(value, key) {
            form[key].$setValidity('sequelize', false);
            this.errors[key] = err.message;
          }, this);
        }
      });
    }
  }
}

angular.module('batnApp')
  .controller('SignupController', SignupController);

Answer №1

Using the ng-change function, I was able to solve this issue by implementing the following functionality:

clearErrors(form, field) {
  if(this.submitted && !form.$valid) { //server-side validation 
    form[field].$setValidity('database', true);
  }
}

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

Dynamic JSX tag including attributes

In my project, I have a set of components stored in a folder named systemStatus. These components are accessible through an index.js file as follows: export UserCount from './UserCount' Additionally, I have a JSX component called Status which i ...

Develop a custom function in Typescript that resolves and returns the values from multiple other functions

Is there a simple solution to my dilemma? I'm attempting to develop a function that gathers the outcomes of multiple functions into an array. TypeScript seems to be raising objections. How can I correctly modify this function? const func = (x:number, ...

Dynamically adjust the height of a parent div to match its child div's height upon clicking using JavaScript

Within my div element (divChatContainer), there is a top div (divChatContainerTop) containing a "span" element. When this "span" element is clicked, I want the container div to resize to the height of the "divChatContainerTop" (essentially minimizing the c ...

Is there a way to interact with specific locations on an image by clicking in JavaScript?

https://i.stack.imgur.com/gTiMi.png This image shows a keypad with coordinates for each number. I am able to identify the coordinates, but I am struggling to click on a specific coordinate within the image using JavaScript. Can someone assist me in achiev ...

What could be causing the catch() block to not execute in Objection.js queries, with the then() method always running and returning either 0 or 1 as a result?

When executing a query using Objection.js, the result of the query will be passed to the then() block as either 0 or 1 depending on its success or failure. Instead of handling errors in the catch block, I find myself checking falsey values. Is there a be ...

The for-loop within the function completes just a single iteration

While the getNew() function contains a loop, it currently only loops once and returns the correct value of 19800 when the month equals 1. I am aiming to modify getNew() to continue looping. For instance, if the month equals 3, it should output: 19800, 19 ...

Angular 2 and Socket.io: Why Does the Event Fire Twice?

Currently, I am working with Socket.io 1.0 in conjunction with Angular 2.0.0-rc.5 and Express 4. In my setup, the 'Emit' function in bin/www is triggering multiple times depending on the number of connected users. This results in each user receiv ...

Guide to verifying a METHOD GET request with Node.js and Express

Issue I've developed an API that returns an array of data when a request is made. The API URL needs to be accessed by an external team of developers. My goal is to restrict access to the /api/verily endpoint only to this specific team. Is there a wa ...

JavaScript if conditions are functional, however the Else statement seems to be malfunctioning

After running this code, I noticed that the if statement under onClick only evaluates when view.checked is true, but the else block does not evaluate when view.checked is false. Despite the alert showing that view.checked is working correctly, I am still p ...

Exploring potentials in OpenLayers by filtering characteristics

Is there a way to filter map features based on their properties? For example, if I have the following property in the geojson: ... "properties": { "Start": 10 } ... How can I make it so that only features w ...

Creating a table with a static first column and vertical text positioned to the left of the fixed column

To create a table with the first column fixed, refer to this fiddle link: http://jsfiddle.net/Yw679/6/. You also need a vertical text to be positioned to the left of the fixed column in a way that it remains fixed like the first column. The disparities be ...

`Cannot Get jQuery ScrollTop Function to Work for 2nd Element`

Having trouble with an issue. Let me explain. I implemented a scrollTop Jquery on page load that scrolls down after a delay to prompt user action. However, I'm facing a problem where another scrollTop doesn't work after clicking buttons "#b3,#b3 ...

What could be causing my Vuex store state to consistently come up empty in middleware when accessing it through SSR (Server-Side Rendering) with NuxtJS?

Question: I am facing an issue with my middleware files. More specifically, I have a file named authenticated.js. In this file, I have a function that checks for authentication. Here is the code snippet: export default function (context) { //console.l ...

Nested states in UI-Router do not necessarily require nested views

I have been struggling to implement a single parent state in Angular UI-Router that several other states can inherit the results of a resolve from. Despite trying various solutions from Stack Overflow, nothing seems to be working for me. This is how my sta ...

Retrieving the value of a basic object

When I receive an object from an API, the data looks like this- { "query1": 0.443} My goal is to extract the number 0.443 by using: request(options, function (error, response, body) { if (error) throw new Error(error); ...

Issues regarding innerHTML

I have a collapsed table with a link that collapses its content. My goal is to change the link (such as from "+" to "-" and vice versa) using JavaScript. I was able to achieve this, but now my table no longer collapses. Here is the code snippet: <div c ...

Retrieving a property of an object within a function

I'm facing an issue where I am trying to access the properties of objects inside an array in my code to display text values in input boxes that are recovered from local storage after a refresh. However, when I attempt to run a for loop within my appSt ...

ID Element Toggle Looping Functionality

Here is a script I have created for toggling (showing and hiding): function btnClass1(where, pval, nval){ var x=$("#btn1-Bln ").val(); if (x == 'Show '){ where.title='Show '; } else{ where.title='Hide '; } where.value=(where ...

When trying to validate an HTML form using AJAX, jQuery, and JavaScript, the validation may not

Here is a high-level overview of what happens: The following code functions correctly... <div id='showme'></div> <div id='theform'> <form ...> <input required ... <input required ... <inpu ...

Launching a Nuxt.js Cloud Function

Is there a possibility to deploy a cloud function that allows for server-side rendering with Nuxt on Firebase? The issue lies in the fact that the Node version used on Firebase is 6.11.1 while the minimum required Node version for Nuxt versions 1 and above ...