What is the best way to ensure my Form stays fresh when I switch tabs?


I've been searching for an answer to this issue for two days now. I'm hoping someone can assist me.

Here is the index HTML code I am working with:

<div class="container-fluid" ng-controller="citizensController as citizensCtrl">
  <h1>Residents</h1>
  <hr>
  <uib-tabset active="activeForm">
    <!-- TAB 1 START -->
    <uib-tab index="0" heading="New">
      <div class="animated fadeIn" data-ng-include='"templates/pages/citizens/form.html"'></div>
    </uib-tab>

    <!-- NEW TAB START -->
    <uib-tab index="1" heading="Report" ng-click="citizensCtrl.reportTab()">
      <div id="grid-citizens" ui-grid="citizensCtrl.gridOptions" class="grid" ng-if="    citizensCtrl.gridOptions.data"></div>
    </uib-tab>

    <!-- TAB 3 STARTS -->
    <uib-tab index="2" heading="View">
      Some Tab Content
    </uib-tab>
  </uib-tabset>
</div>

And here is the form HTML code:

<form name="citizenForm" ng-submit="citizensCtrl.createCitizen(citizenForm)" class="css-form" novalidate>
  <div class="form-group">
    <label for="citizen_name">Name *</label>
    <input type="text" class="form-control" id="citizen_name" placeholder="Name" ng-model="citizensCtrl.citizen.name" required>
  </div>

  <div class="form-group">
    <label for="citizen_birthday">Birthday *</label>
    <uib-datepicker ng-model="citizensCtrl.citizen.birthday" class="well well-sm" datepicker-options="citizensCtrl.dateOptions" required></uib-datepicker>
  </div>

  // Remaining form elements and input fields...

  <input type="submit" class="btn btn-primary" value="Create" />
  <input type="reset" class="btn btn-default" ng-click="citizensCtrl.defineCitizen(); citizenForm.$setPristine()" value="Clear Form" />
</form>

Additionally, this is the function in my AngularJs controller that gets triggered:

  self.createCitizen = function(form){
    if(form.$valid){
      $http.post(apiUrl + endpoint, angular.extend({},self.citizen)).then(function(response){
        alertsService.add('success', 'Resident created successfully!');
        form.$setPristine();
        self.defineCitizen();
      }, function(error){
        alertsService.add('danger', 'Oops.. Something went wrong. Please contact the administrator.');
      });
    }else{
      alertsService.add('danger', 'You need to fill out all required fields.');
    }
  };

The issue arises when submitting the form with errors such as missing required items. The CSS changes to indicate the errors, but switching tabs and returning to the form tab does not clear the error styling on the inputs. It seems like the form remains dirty or something similar.

Answer №1

In order to guide users effectively, I implemented a ng-if directive within the form. When the user navigates away from the "Create" tab, the form is deleted due to the ng-if. As a result, when they return to the "Create" tab, the form is reconstructed.

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

The clarity and completeness of images on canvas are lacking

I've experimented with numerous examples found on the internet, but none of them seem to be effective. My goal is to display a full image on a canvas without it being zoomed in and losing quality. In addition, I attempted to rotate images (from stand ...

How is it possible that both of my AngularJS services are being directed to the exact same Restful Java EE web service?

I am facing an issue with my two angularjs services. One is supposed to retrieve a single user while the other should return an array of users. However, both seem to be calling the service that returns an array of users. Can you help me understand why this ...

Bootstrap Navigation Bar Animation causing Input Fields to be cut off

I am working with a collapsible bootstrap NavBar that contains two elements within the collapsible <div>, <form class="form-inline> and <select class="custom-select" id="menu"> (Refer to my code below) The problem I'm encountering ...

Automatically submit form in Javascript upon detecting a specific string in textarea

Just getting started with JS and I have a question that's been bugging me. I have a simple form set up like this: <form method="POST" action="foo.php"> <textarea name="inputBox123"></textarea> <input type="submit" value="Go" name ...

Is it possible to rearrange an array by moving an element to the front

If I have an array as shown below, how can I move key [2] and its corresponding value to the front of the array? This would make it key [0] and increment the other keys by 1. Current: [0] => Array ( [name] => Vanilla Coke cans 355ml x 2 ...

Arrange symbols in fontawesome into a pile

Is there a way to stack grid icons in the font awesome framework to create a 3x2 grid icon? Demonstration Jsfiddle <a href="./"><i class="fa fa-square"></i> 1x1</a> <a href="./" ><i class="fa fa-square"></i>< ...

Adjusting the Size of Slideshow Pictures in a JavaScript Array to Match the Viewport

Welcome to my first post! Please excuse any formatting issues. I am currently working on a project where I am trying to create a slideshow using Javascript. I have successfully added images into an array and have the slideshow up and running. However, I ...

Display an AJAX div when the image is hovered over and have it track the movement of

I am seeking assistance with my website project, which involves providing users with download links to movies. However, I am struggling to make the preview_block div(id) appear when the mouse hovers over the movie_block div(id). Additionally, I am having ...

How can I effectively vertically position a button in Bootstrap 5 without compromising its functionality?

I'm attempting to reposition the sign-up buttons for Google, Apple, and email below where it says "join," but they stubbornly refuse to budge even when I try to force them with CSS. https://i.sstatic.net/74LQk.jpgCSS .homepage_topRight_Buttons{ ...

Begin loading your script file at the conclusion of the content load in AngularJS

I have implemented the script below: $rootScope.$on('$includeContentLoaded', function (eve,uri) { var js = document.createElement("script"); js.type = "text/javascript"; js.src = '/assets/js/scripts.js'; ...

Determine the specific assertions that have caused the protractor test to fail

Is it possible to identify the specific test that failed when a protractor test fails? For instance, if the output only shows: 1 test, 1 assertion, 1 failure When there are multiple assertions and failures, identifying which one failed can be challengin ...

Determine the total character count in every paragraph

Looking for a way to tally up the number of characters in each paragraph on a webpage. Came across this handy snippet that counts words in paragraphs and it works like a charm. Wondering if it can be tweaked to also calculate the character count. There are ...

The dropdown navigation bar fails to close upon being clicked

I'm currently facing an issue with the navbar in my project. The bootstrap navbar doesn't close automatically after clicking on a link. I have to move my mouse away from the navbar for it to close, which is not ideal. Additionally, I'm worki ...

HTML unique flex wrapping

https://i.sstatic.net/ekVr4.png #menu { display: flex; justify-content: space-evenly; align-items: center; margin: 0 5px 0 5px; height: 30px; flex-wrap: wrap; } .menu_item { margin: 0 10px 0 10px; } .menu_item2 { margin: 0 10px 0 10px; } <div id="m ...

Enhancing the padding of submit buttons in Mac OS X

I'm struggling to apply padding to a submit button. I have created a simple example in a JSFiddle: http://jsfiddle.net/yxr197pa/1/ Below is the code for it: HTML: <input type="submit" value="Submit" /> CSS: input { padding: 20px; } I ...

Positioning Text in CSS/JS Navigation

Looking for assistance on aligning the text in the menu located at: 1st tab: I would like a line break after "Item". 2nd and 3rd tabs: I am seeking a line break after "This is". I have only included the section of the menu that requires adjustment, whi ...

Navigate the scroll bar horizontally one by one instead of all at once, due to a width problem

The issue I'm facing involves determining the correct width for scrolling left or right. This results in the navigation tabs not scrolling correctly one by one. Each time I click the scroll right button, it adds more width than necessary and causes th ...

Creating a personalized bullet text box with HTML and Javascript: A step-by-step guide

I have been working on creating a customized text box using HTML and JavaScript that has specific requirements: The text box should start with a single bullet point, like this: https://i.sstatic.net/U7pHo.png Each new line entered by the user should autom ...

Search the database using a single text field and then automatically fill in the remaining fields using PHP

Being a beginner in PHP and Ajax, I find myself quite lost in the process. I have set up a MySQL database and created several forms in PHP. One of the forms is supposed to display customer information with text fields, including a search field for phone nu ...

Why does Three.js change the innerWidth and innerHeight of a div on its own after being set with the width and height of a plane?

Greetings, I have a question. In my attempt to adjust the innerWidth and innerHeight of the div container which houses a canvas element, I am using the code snippet below. The crucial part is the loader.load function that gets executed upon completion of ...