Exploring AngularJS capabilities in showing server validation messages along with creating dynamic forms

On the server side, I am utilizing Express along with sequelizejs.

Let's say we have this particular view: (It is included as

<div ng-include src="'views/users/_form.html'"></div>
in the new, edit view.)

<div class="form-group">
  <label for="userLastName">Last Name</label>
  <input type="text" class="form-control" id="userLastName" name="lastName" placeholder="Last name" ng-model="$parent.user.lastName">
   <!-- how to display a server validation message here? -->
</div>

As well as the following controller code that is tasked with creating the user.

User.create($scope.user).$promise
  .then(function () {
    $location.path('/users')
    $scope.users = rePlanApi.User.query()
  })
  .catch(function (err) {
    _.forEach(err.data, function (errors, key) {
      if (key !== '__raw' && _.isArray(errors)) {
        _.forEach(errors, function (e) {
          $scope.user[key].$dirty = true
          $scope.user[key].$setValidity(e, false)
        })
      }
    })
  })

In case of a validation error, the server will return a 422 status code and a sample response might look like:

{"lastName":["Lastname fails to meet a certain requirement", "Something else"]}

While Angular can handle some validations, certain checks such as uniqueness of emails require server-side validation.

Since Angular does not have models, the user in $parent.user.lastName is simply a basic Javascript object.

How can I attach a validation message to $parent.user.lastName and present it on the frontend?

The mentioned code has a few issues:

  • If the form is submitted empty, then $parent.user is undefined
  • If a field is left blank, then $parent.user.fieldinquestion is undefined

My ultimate goal is to streamline the code similar to what is offered by plataformatec/simple_form

Is there a library in Angular that provides a similar helper functionality? Or do I need to create my own using Angular directives?

Thank you

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

Using Plotly.js within a deleted Vue.js component may result in displaying an incorrect chart

Issue I am facing a problem with deleting one of the components that contains a chart. Even after deleting the component, the chart remains visible. To see an example, check out this jsfiddle: https://jsfiddle.net/m4ywp5fc/4/ Solution Attempted I attem ...

Creating JSON arrays for multiple elements using the same model name

I am using Angular to generate JSON data for multiple elements. Currently, the output looks like this: {"field":{"name":{"0":"a","1":"b"},"length":{"0":10,"1":20}}} However, I would like the JSON data to be more sophisticated and in array form, like this ...

Utilizing the nested combination of map and filter functions

Struggling to grasp the concept of functional programming in JavaScript, my aim is to extract object boxart items with width=150 and height=200. Console.log(arr) the output shows [ [ [ [Object] ], [ [Object] ] ], [ [ [Object] ], [ [Object] ] ] ] I&apos ...

Is there an easy method for extracting URL parameters in AngularJS?

Hello, I'm new to Angular and coming from a background in PHP and ASP. In those languages, we typically read parameters like this: <html> <head> <script type="text/javascript"> var foo = <?php echo $_GET['foo&apo ...

"Combining AngularJS with Material Design disrupts the functionality of infinite scroll

Issue: Infinite scroll is loading all pages at once instead of waiting for the user to scroll to the page bottom. Environment: AngularJS 1.3.17 Materials Design 0.10.0 Infinite scroll script: https://github.com/sroze/ngInfiniteScroll Demo being used: The ...

Obtain the IP address of a Node application running within a Docker container

I currently have a node express application set up in a Docker container, and I am attempting to record the IP address of each incoming request within the app. However, due to running behind a firewall, my current method "req.headers['x-forwarded-for& ...

SmartCollection in Meteor generating unpredictable outcomes

When executing News.insert({name: 'Test'}) in the browser JS console, it caused {{count}} to increase from 0 to 1. Checking in mongo console using mrt mongo, db.news.find().count() also returns 1. However, after adding a record through the mongo ...

The following middleware is not functioning properly on a local SSL server

When I run my Nextjs app without SSL using "next dev", the middleware functions as expected without any errors. However, if I attempt to run the app with SSL enabled, an empty middleware function triggers an error. The code for the middleware function (l ...

In search of a render function that can effectively apply styles to HTML strings using React JS

I have been struggling to convert the result field value, including HTML attributes string, into respective styles for one column in my antd table. Below is the string I am trying to convert: The exhaust air requirements for fume hoods will be based on m ...

Unexpected behavior exhibited by DOM elements

I can't seem to figure out this issue. Every time I run this loop: for ( var i=0; i < 10; i++ ) { var $items = $(balls()); console.log($items); $container.imagesLoaded(function(){ $container.append( $items ).masonry( 'app ...

The Angular model finally refreshes its values after a console.log() is executed

UPDATE After further investigation, I discovered that the issue was not related to Angular itself, but rather a mistake in the update function within the node server controller. I have provided the fix below for reference, and decided to keep this questio ...

Is there a way to make the first Image Element within the div automatically clickable?

Is there a way to set the first image in a div as the default clicked element? I have a div with multiple images and I want the first one to be clicked by default. This is part of a React functional component that serves as a view. <div className=&quo ...

Different or improved strategy for conditional rendering in Angular

Hey there! I've got a few *NgIf conditions in my template that determine which components (different types of forms) are displayed/rendered. For example, in one of my functions (shown below) private _onClick(cell:any){ this._enableView = fals ...

Vue Router generates a URL containing %2F when dealing with slug routes

I am facing an issue in my Vue 3 application where I need to create a route for dynamic slugs. Currently, when using RouterLink, the generated URL contains %2F instead of actual slashes which is not the desired result. For example, the current URL looks l ...

Ways to access the props value within the lifecycle hooks of Vue JS

Is there a way to retrieve the value of props using lifecycle hooks such as mounted or updated, and then store that value in my v-model with additional text? I've been struggling to achieve this. I attempted using :value on the input element with bot ...

AngularJS select box plugin exclusive to use

Which select box for Angular is recommended without relying on jQuery? I have come across many select boxes using jQuery, but this time I am working solely with Angular. Appreciate the insight! ...

Troubleshooting issues with JavaScript progress bar functionality

I have implemented a progress bar using HTML5, JavaScript and Ajax to showcase file uploads in PHP. The issue I am facing is that the progress bar is not displaying the progress correctly. In addition to that, the echo statements in the PHP code are no ...

Unable to execute jQuery function from JavaScript

I have been researching this issue and while I found similar posts, none of the solutions seem to work for me. My situation is slightly different. Within my code, I have a javascript function called addFormField that creates a dynamic input element within ...

Spring MVC: Incorporating Java Map into Optgroup Options

In my Spring MVC project, I am currently passing a Map from Java to my webpage using the variable "userLocales" through request.setAttribute('userLocales', bluh bluh bluh). I am looking for a way to extract the strings from this variable and use ...

Enhancing Online Presence with Video Gallery Website Development

I'm in the process of creating a website and need help finalizing my video page. I envision a layout similar to this example: https://i.stack.imgur.com/hctui.gif The main feature should be a large video placeholder at the top, followed by several thu ...