After the modal is closed, the form object becomes devoid of any content

Having two forms in separate modals has led to an interesting observation. Upon opening the first modal, I successfully log the form attached to $scope. The controller initially assigns $scope.updateSecurityForm = {} with the name attribute as name="updateSecurityForm". All the expected properties like $valid are present.

However, encountering a peculiar situation when opening the second modal and logging the form from the first modal results in an empty object. Subsequently, closing the second modal and reopening the first modal leads to the same outcome of an empty object. This inconsistency was unexpected as the form properties should have been retained.

The configuration of the first modal form is as follows:

<div class="modal-body">
  <form name="updateSecurityForm" ng-submit="updatePersonnels(upObj, updateSecurityForm)" ng-repeat="upObj in personnelProfileData" novalidate>
    <div class="col-md-4">
      <label for="vendor-name" class="control-label">Vendor Name:</label>
      <select name="vendorname" ng-model="upObj.vendor_id" class=" text-center form-control" ng-options="data.vendor_id as data.vendor_name for data in getvendetailsdata">
        {{data.vendor_name}}
      </select>
    </div>
  </form>
</div>

As for the second modal:

<div class="modal-body">
  <form name="addPlumberForm" ng-submit="addPersonnelsByRole(addObj,8, addPlumberForm)" novalidate>
    <div class="col-md-4">
      <div class="form-group">
        <label for="message-text" class="control-label">
          Name of the vendor:<span style="color: red;">*</span>
        </label>
        <select class="text-center form-control ng-valid ng-empty ng-dirty ng-touched" name="vendorname" ng-model="addObj.vendor_id">
          <option value="" selected="selected">- Please select a vendor name -
          </option>
          <option ng-repeat="ae in getvendetailsdata" value="{{ae.vendor_id}}" class="ng-binding ng-scope">{{ae.vendor_name}}</option>
        </select>
      </div>
  </form>
  </div>
</div>

Both modals exist on the same page and share similarities, including the same name attributes in the forms for validation convenience.

In the controller, the initialization includes

$scope.updateSecurityForm = {} and $scope.addPlumberForm = {} for reference.

During validation, encountering

Cannot read property '$invalid' of undefined
when utilizing

$scope.addPersonnelValidation = function(formName) {
    console.log(formName, "formName");
    if (formName.vendorname.$invalid) {
      $.bootstrapGrowl('Please select vendor name', {
        type: 'danger',
        delay: 2000,
      });
      return;
    }

Execution of the validation function is done through:

$scope.addPersonnelsByRole = function(addObj, user_role_type_id, formName) {
  if ($scope.addPersonnelValidation(formName)) {
    //valid form
  }
}

Answer №1

After compiling all of your code in jsfiddle, I did not encounter any undefined errors. However, there are a few corrections that need to be made for your validation to function properly. You can find the jsfiddle link below for reference. Please let me know if this is helpful.

  • The required attribute is missing in the second modal's select element.
  • The closing div tag is missing above the form tag in the second modal.

<div class="modal-body">
   <form name="addPlumberForm" ng-submit="addPersonnelsByRole(addObj,8, addPlumberForm)" novalidate>
      <div class="col-md-4">
         <div class="form-group">
            <label for="message-text" class="control-label">Name
            of the vendor:<span style="color: red;">*</span>
            </label>
            <select
               class="text-center form-control ng-valid ng-empty ng-dirty ng-touched"
               name="vendorname" ng-model="addObj.vendor_id" required>
               <option value="" selected="selected">- Please
                  select a vendor name -
               </option>
               <option ng-repeat="ae in getvendetailsdata"
                  value="{{ae.vendor_id}}" class="ng-binding ng-scope">{{ae.vendor_name}}</option>
            </select>
         </div>
         <input type="submit">
         </div>
   </form>
</div>

jsfiddle link : https://jsfiddle.net/anilsarkar/txbmg2wy/3/

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

Check for the presence of a specific variable before using it as a dependency in useEffect. Only watch it if the variable

Here is the code snippet I'm currently working with: useEffect(() => { if (field[1].isActive === true) { handleMore(); } }, [field[1].text]); One issue I'm encountering is that sometimes the Field data does not come in the json-response, ...

"Discover the step-by-step process of transforming an input field value into a checkbox with

I've been experimenting with creating a To-Do list using HTML, CSS, and Javascript. I've managed to capture the input field value in a fieldset so far. However, my goal is to find a way to transform the input field value from the textfield into a ...

Codemirror - Advanced Auto-Suggest Feature with Separator

Can a separator be easily added in the hints/autocomplete addon? This separator would help transform the suggestion box to look like: f1 f2 f3 --- var1 var2 ...

Jquery is not working as expected

I am having trouble implementing a jQuery function to show and hide select components. It doesn't seem to be working correctly. Can someone help me identify the issue? <html> <head> <meta charset='UTF-8' /> <script ...

The AJAX response is shown just a single time

My code is designed to send an ajax request when a form is submitted, specifically a search module. It works perfectly the first time the form is submitted, highlighting the table when data is returned. However, I am only able to see the effect once, as th ...

angular $routeSegmentProvider navigate through route paths

When utilizing $routeSegmentProvider, I inject $routeSegment into my controller. This provides information about the route used to construct the current page (chain property). To further enhance my service, I am searching for a method to generate a compl ...

Tips for retrieving text box information from a ng-repeat table and transferring it to an array

<div> <div class="row"> <h4 style="margin-left: 45%; color: black; text-decoration: underline"><b>Inter Equalent Education</b></h4> </div> <div class="row"> <div class="col-md-4"> &l ...

Creating a dynamic Bootstrap carousel with a fixed hero header text inside a designated container - here's how!

I am currently working on a project to develop a responsive Bootstrap slider with a fixed hero header that remains consistent while the slider images change. The hero header needs to be aligned to the left within a responsive Bootstrap container and center ...

An essential component of Chai assertion is the inclusion of "or"

While attempting to iterate through a list of elements, I am looking to verify that the body contains either of two values. However, the current assertion method only allows me to check for one value: expect(cellText).includes(toDateFormat); Is there a wa ...

Discrepancy Between Angular JS $resource POST Response and Server Output

I am currently working with a resource factory in my project that utilizes a POST method called update: PnrApp.factory('Feed', function ($resource, $cacheFactory, $q, $rootScope) { var Feed = $resource('api/feeds/:post', { post: ' ...

How to download a file using AJAX in Laravel?

Is there a way to download a CSV file within an ajax call? I have an ajax request in my Laravel controller that successfully retrieves the file contents in the response. However, I am facing issues with actually downloading the file. Laravel controller c ...

Encountering the "No injector found for element argument to getTestability" error while navigating between various single page applications

Currently, I am conducting tests on Protractor for a website that is bootstrapping AngularJS manually. Despite the steps continuing to execute, I encounter this error: Error while waiting for Protractor to sync with the page: "[ng:test] no injector found ...

Executing system commands using Groovy is a breeze

One of the scripts I have is a sample.js script that allows me to view files located on the server myHost. It works perfectly: var exec = require('ssh-exec') var v_host = 'myHost' exec('ls -lh', { user: 'username&apo ...

Steer clear of dividing words

I am attempting to showcase sentences letter by letter with a fade in/fade out effect. However, I am facing an issue where words break in the middle. How can this word breaking be prevented? var quotes = document.getElementsByClassName('quote' ...

I am concerned about the security of my games as they can be easily hacked through right-click inspect. What measures can

As a newcomer to game development, I am creating web games using JS, HTML, and CSS. However, I have encountered an issue with preventing right-click inspect hacking, which has led to people hacking my games through this method. I am wondering, how can I s ...

Alter the class of the div element every three seconds

Greetings, I trust everyone is doing well. I am in need of some assistance from your brilliant minds. I have a circular div along with three CSS classes, and my objective is to switch the div's class and update the label text every 3 seconds. Any insi ...

What is causing the ERR_HTTP_HEADERS_SENT('set') error when running on Windows Server, but not on my development machine?

Currently, I am in the process of developing a Node/Express application that will integrate with ActiveDirectory. The login form is designed to post the username and password to a specific /auth route, where the AD authentication takes place, along with se ...

What is the best way to create a JSON string using JavaScript/jquery?

Is there a way to programmatically build a JSON string? The desired outcome should resemble the following: var myParamsJson = {first_name: "Bob", last_name: "Smith" }; Instead of constructing the entire object at once, I would prefer adding parameters one ...

Angular event not refreshing the data

Just starting out with Angular, I was advised against using jQuery alongside it. However, there are times when I find jQuery more convenient to use. Recently, I created a function to fetch a list of comments. function displayComments(){ app.co ...

I am interested in developing a JavaScript program that can calculate multiples of 0.10

I am looking to let users input values that are multiples of 0.10, such as - 0.10, 0.20, 0.30....1.00, 1.10, 1.20...1.90, and so on. When a user enters a value in the text box, I have been checking the following validation: amount % 0.10 == 0 Is this a ...