Styling errors in AngularJS validation code

I am facing an issue with the code below that generates 3 text boxes. When I click on one text box, all 3 of them get focused, despite having different name and label values.

<div class="col-md-12" data-ng-repeat="dohPolicy in [1,2,3]">

    <div class="col-md-2" style="padding:0px; margin-right:1%;">
        <p style="font-size:11px;">P no {{dohPolicy}} </p>
    </div>
    <div class="form-group col-md-6"  
            data-ng-class='{ "has-focus": dohForm.dohPolicy.hasFocus,
                             "has-success": dohForm.dohPolicy.$valid,
                             "has-error": dohForm.dohPolicy.$invalid && (dohForm.$submitted || dohForm.dohPolicy.$touched),
                             "is-empty": !dohForm.dohPolicy.$viewValue }' 
                             style="right: 150px; bottom: 40px; padding: 0;  width:20%;">
        <label for="dohPolicy"></label>
        <input type="text" name="dohPolicy"
                    data-ng-model="dohPolicy" required readonly
                    data-ng-blur='dohForm.dohPolicy.hasFocus=false'
                    data-ng-focus='dohForm.dohPolicy.hasFocus=true'>
        <p data-ng-show="dohForm.dohPolicy.$error.required && (dohForm.dohPolicy.$touched || submitted)"
                class="error-block">
           P Number(s)
        </p>
    </div>
</div>

Even after trying to use string arrays, I haven't been able to resolve this issue. I would like only the clicked text box to be focused and highlighted.

Any assistance is welcomed!

Thank you

Answer №1

<input type="text" name="dohPolicy"
                                data-ng-model="dohPolicy" required readonly
                                data-ng-blur='dohForm.dohPolicy.hasFocus=false'
                                data-ng-focus='dohForm.dohPolicy.hasFocus=true' 
                                >

All dynamically generated input texts here share the common model dohPolicy.

As a result, they all become dirty simultaneously and trigger the blur & focus events.

Ensure to assign a unique name for each model.

Answer №2

Utilize $index in your naming convention and perform dynamic validation

<div class="col-md-12" data-ng-repeat="dohPolicy in [1,2,3]">

    <div class="col-md-2" style="padding:0px; margin-right:1%;">
        <p style="font-size:11px;">Policy no {{dohPolicy}} </p>
    </div>
    <div class="form-group col-md-6"  
    data-ng-class='{ "has-focus": dohForm.dohPolicy.hasFocus,
        "has-success": dohForm.dohPolicy.$valid,
        "has-error": dohForm.dohPolicy.$invalid && (dohForm.$submitted || dohForm.dohPolicy.$touched),
        "is-empty": !dohForm.dohPolicy.$viewValue }' 
        style="right: 150px; bottom: 40px; padding: 0;  width:20%;">
        <label for="dohPolicy">
        </label>
    <input
        type="text" name="dohPolicy_{{$index}}"
        data-ng-model="dohPolicy" required readonly
        data-ng-blur='dohForm.dohPolicy.hasFocus=false'
        data-ng-focus='dohForm.dohPolicy.hasFocus=true' 
        >
        <p
        data-ng-show="dohForm['dohPolicy_' + $index].$error.required && (dohForm.dohPolicy.$touched || submitted)"
        class="error-block">Policy Number(s)</p>
    </div>

</div>

Answer №3

As per @Riyaj's suggestion, all input controls share the same model and name, causing them to all become dirty simultaneously.

It is crucial for each control within a form to have a unique name.

Therefore, it is necessary to assign different names to individual input controls.

<form role="form" novalidate name="vm.form">
  <div class="col-md-12" data-ng-repeat="dohPolicy in [1,2,3]">
      <div class="col-md-2" style="padding:0px; margin-right:1%;">
          <p style="font-size:11px;">P no {{dohPolicy}} </p>
      </div>
      <div class="form-group col-md-6"  
      data-ng-class='{ "has-focus": dohForm.dohPolicy[{{dohPolicy}}].hasFocus,
              "has-success": dohForm.dohPolicy[{{dohPolicy}}].$valid,
              "has-error": dohForm.dohPolicy[{{dohPolicy}}].$invalid && (dohForm.$submitted || dohForm.dohPolicy.$touched),
              "is-empty": !dohForm.dohPolicy[{{dohPolicy}}].$viewValue }' 
              style="right: 150px; bottom: 40px; padding: 0;  width:20%;">
          <label for="dohPolicy">
          </label>
      <input
          type="text" name="dohPolicy[{{dohPolicy}}]"
          data-ng-model="dohPolicy" required
          data-ng-blur='dohForm.dohPolicy[{{dohPolicy}}].hasFocus=false'
          data-ng-focus='dohForm.dohPolicy[{{dohPolicy}}].hasFocus=true' 
          class="form-control"
          >
          <p
          data-ng-show="dohForm.dohPolicy[{{dohPolicy}}].$error.required && (dohForm.dohPolicy[{{dohPolicy}}].$touched || submitted)"
          class="error-block">P Number(s)</p>
      </div>
    </div>
</form>

I trust this information will be beneficial to 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

Submit the scaled-down form image to the server

When attempting to upload a resized image to the server, an error stating "Required MultipartFile parameter 'file' is not present" occurs. Interestingly, this error only appears when trying to upload the resized image, as uploading the original f ...

An error has occurred: [$injector:modulerr] Unable to create module xxApp because: Error: [$injector:nomod] Module 'xxApp' does not exist

After running the gulp task to uglify my application controller files, I encountered an error. In a previous project, the same gulp uglify code worked without any issues, but that project did not include any ES6 commands. Now I am using the 'gulp-ugl ...

Using either AngularJS's simple .then method or the $q service to handle asynchronous requests

I'm trying to understand the distinction between AngularJS $q service and simply using .then() after an asynchronous request. For example, using .then(): function InboxService($http) { this.getEmails = function getEmails() { return $http.get(& ...

Can someone explain how I can use Selenium and Python to choose an available date using a calendar picker?

Having a class named .calendarCellOpen: table.calendario .calendarCellOpen input { } This is the CSS for the calendar: #calwrapper { min-height:230px; margin-top:10px; } #calendar{ float:left; marg ...

Aligning three hyperlinks evenly to spread across the webpage

My professor provided the class with an image that may resemble something he could ask us to recreate on an upcoming exam. He suggested we try replicating it at home to study. I have most of it figured out, but there are three links positioned perfectly ac ...

In the past, I've crafted buttons and other elements using Photoshop, and subsequently saved them for web use. However, I'm now contemplating whether it's more advantageous to employ CSS in

In the past, I have utilized Photoshop to create visually appealing buttons and footer bars for my websites. However, I'm now pondering if it's more beneficial to utilize CSS for button creation. I've noticed that manually coding them can re ...

The positioning in CSS is not functioning as expected in a customized component

https://codesandbox.io/s/71j1omvz66 Struggling to shift the chat component to a new position. Any ideas on how to make it move? ...

Laravel 4 not receiving data from Angular $http PUT request in 'multipart/form-data' format

Hello there! I am currently working with a RESTful API created in Laravel 4 and using Angular.js for handling tasks on the frontend. The main goal is to allow users to create a new 'item' in the database by submitting form data via POST request ...

An issue has occurred while utilizing Angular

I'm diving into the world of Angular and encountering some errors as I try to execute this code. An unexpected token is causing trouble. A constructor, method, accessor, or property was expected. The left side of a comma operator seems to be unused ...

Is there a way to delete a wrapper (parent element) in Angular without deleting the child element as well?

The solution provided in this answer on Stack Overflow addresses jQuery and not Angular or TypeScript. My inquiry bears resemblance to this question raised on a forum, but I am specifically looking for a resolution within the context of Angular. Is there ...

Guide to retrieving a user's current address in JSON format using Google API Key integrated into a CDN link

Setting the user's current location on my website has been a challenge. Using Java Script to obtain the geographical coordinates (longitude and latitude) was successful, but converting them into an address format proved tricky. My solution involved le ...

Elegant URLs using AngularJS and .htaccess

I've been grappling with this issue for a whole week now. Utilizing AngularJS, I have enabled html5mode(true) to eliminate the necessity of the hash sign "#" in the URL. All components seem to be functioning properly and my URL logic is in sync with Q ...

The formatting of the list is not being correctly displayed in the Ajax Jquery list

Trying to dynamically generate an unordered list from XML onto a jQuery mobile page has been a bit of a challenge for me. While I can display the items on the page, the styling never seems to work properly, only showing up as plain text with blue links. Is ...

Is the HTML5 capture attribute compatible with wkwebview?

Is it supported as stated in the title? For more information, you can check out the MDN documentation (Incomplete) and the Caniuse list of supported browsers. Surprisingly, none of them mention wkwebview. Does this attribute have support in wkwebview? Kno ...

Unable to remove the vertical dropdown menu padding issue in Firefox

issue: https://i.stack.imgur.com/qisVn.jpg My website appears correctly on IE, Opera, and Chrome, but in Firefox, there is a spacing problem between each image in the dropdown menu. I suspect the issue lies within the CSS code. The same spacing issue occ ...

No results found for the xpath within the <p> tag

I'm currently using selenium.webdriver chrome to scrape data from my website, and I need to extract the location details of visitors. Although I have successfully identified the <p> tag that contains 'location', it is returning None. ...

Is Javascript the best choice for managing multiple instances of similar HTML code?

I am currently facing the challenge of dealing with a lengthy HTML page consisting of around 300 lines of code. The majority of the code involves repetitive forms, each identical except for the ID (which varies by number). Is it considered appropriate or ...

What is the best way to showcase the outcome on the current page?

This is a sample HTML code for a registration form: <html> <head></head> <body> <form id="myform" action="formdata.php" method="post"> username:<input type="text" name="username" id="name"><br> password:&l ...

Changing CSS properties in React

Currently, I am working on a dynamic table component using CSS grid. My goal is to customize the grid-template-columns CSS property. I am familiar with using repeat in CSS to achieve this. However, I am facing a challenge when it comes to dynamically chang ...

Rows within distance

There seems to be too much space between the grid rows. I tried adding more photos or adjusting the height of the .image class to 100%, but then the image gets stretched out. The gap between items with classes .description and #gallery is too wide. When ...