I am looking to modify my CSS class in PrimeNG and Angular 14 depending on the presence of an error in my p-dropdown component. Can anyone guide me on how to

I am currently using PrimeNG and Angular 14. My goal is to add a red border class to my p-dropdown component when there are errors in the form control, and apply another class if there are no errors. I attempted the following:

      <p-dropdown
      styleClass="border-round-md"
      [ngClass]="{
        'border-red-500':
          submitted && !this.form.get('myName')?.valid
      }"
      [options]="cycleFrPerMonth"
      formControlName="myName"
    ></p-dropdown>

Unfortunately, this approach didn't work as expected. The error class doesn't appear even when there are errors present. I also tried replacing the condition with the word "true", but that didn't solve the issue either.

Answer №1

Here is a helpful solution for your issue:

  1. When using ngClass with p-dropdown, apply your class to the first div inside the component by using the styleClass input.
  2. Include your class with /deep/ in the SCSS file of your component like this:
:host ::ng-deep .border-red-500{
 border-color: #007bff !important;
}

Instead of ngClass, utilize styleClass as shown below:

<p-dropdown
  [styleClass]="submitted && !form.get('myName')?.valid 
   ? 'border-red-500' : 'border-round-md' "
  [options]="cycleFrPerMonth"
  formControlName="myName"
></p-dropdown>
  1. In your style file:
:host ::ng-deep .border-color {
  // /*your style here */
}

:host ::ng-deep .border-round-md {
   // /*your style here */

}

This should resolve the issue effectively.

Answer №2

To start, make sure to check angular.json to confirm that the primeflex.css style has been added.

styles:[...,"node_modules/primeflex/primeflex.css"] 

        

If it is not there, you can install it using: npm install primeflex --save and then import it into the styles array in angular.json

Next, add a red color to your Primeng palette in the style.css file:

:root {
  --red-50: #fff5f5;
  --red-100: #ffd1ce;
  --red-200: #ffada7;
  --red-300: #ff8980;
  --red-400: #ff6459;
  --red-500: #ff4032;
  --red-600: #d9362b;
  --red-700: #b32d23;
  --red-800: #8c231c;
  --red-900: #661a14;
}

Finally, here is an example link to meet your requirements: stackblitz

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

Is it possible to consolidate all CSS and JS links into a single PHP file for easier management?

Having spent years working with spaghetti code in my PHP projects, I've decided to make the switch to Code Igniter to gain experience with an MVC Framework (even though I know it's not as popular now, Laravel/Composer blew my mind). I have an ol ...

Navigational menu aligned vertically alongside a horizontal subheading

I am in need of assistance with my website's navigation. I am looking to implement a horizontal navigation bar that, when hovered over, will reveal a dropdown sub-navigation with various sections (left and right alignment). Each section will have two ...

Any advice on how to navigate to a different screen based on the content of a dynamic grid view?

I have a grid view displaying data from my database, with each title representing a separate box. When a title in the grid view is clicked, I want to redirect to a corresponding screen. How can this be achieved? For example, clicking on "Orders" should re ...

How can I customize the styling of Autocomplete chips in MUI ReactJS?

Trying to customize the color of the MUI Autocomplete component based on specific conditions, but struggling to find a solution. Any ideas? https://i.stack.imgur.com/50Ppk.png ...

The alignment of the image background is malfunctioning on the iPad 3 device

Hey there, I need some assistance resolving an issue with a header background image on iPad3. The problem is that the image is not displaying correctly on iPad3 browsers, even though it works fine on desktop browsers. Below you can find my CSS and HTML cod ...

Challenges with Div Height and Border

Hello there! I've run into a height and border issue with a div. Here's the CSS I'm currently working with: .content { background-image: url(../../images/logo-04.png); background-position: left bottom; ...

Navigating the World of CSS Selectors - Balancing Style Control Without Excessive Class Usage

I recently stumbled upon a responsive menu that I wanted to incorporate into my webpage design. However, as I delved deeper into the implementation process, I realized that the CSS code associated with the menu was targeting all ul, li, a elements on the e ...

Challenge with jQuery UI Draggable: Div moves slower than mouse cursor

My issue is that when I move the mouse, the mouse moves faster than the div underneath it. I am using jQuery UI 1.12.1 and jQuery 3.3.1 with default options. Here is my HTML: <div class="popup"></div> And here is my jQuery code: $(function( ...

Mastering the art of using div boxes in boxing form

When I box in information using divs, I've noticed that sometimes the wrapping boxes don't fill out the space completely. This can result in elements from other wrappers encroaching into the box area. For example: <div> <div style="le ...

Applying the .active class to a button which reveals the corresponding div

I've been working on a menu that functions to open and hide divs within a single page. My objective is to have the (.active) class applied to the currently selected menu item only when its connected div is open. When the div is closed or another menu ...

Fetching data in VueJs before redirecting to a new page

Within the mounted function, I am creating an action that fetches data from a Rest API and populates my table in a Vue.js component mounted() { UserService.getProjects().then( (response) => { this.isProject = true; this.project ...

Apply borders to inline-block elements uniformly

Click here for the code snippet Can anyone suggest a solution to adding borders to inline-block divs in a way that avoids repetition and ensures equal border width on each side of the box? I am trying to create a calendar layout. #parent{ width: 400p ...

What is the most efficient way to incorporate or import CSS from a CDN into a Create React App without needing to eject?

After reviewing this answer, I can't help but wonder if there's a more efficient method for importing a CSS file into a React component, similar to how JS files are imported. Perhaps something akin to SCSS's @import url('cdn-url.css&ap ...

Using jQuery to store the last selected href/button in a cookie for easy retrieval

Recently, I've been working on a document that features a top navigation with 4 different links. Each time a link is clicked, a div right below it changes its size accordingly. My goal now is to implement the use of cookies to remember the last select ...

Creating a mobile-friendly layout with 3 columns: Tips and tricks

I attempted to solve the issue independently but didn't have much success. My goal is to create a proper version for mobile users using bootstrap, however, it currently looks terrible on mobile devices. I suspect the problem lies with the div classes ...

Incorporating fresh CSS styles through Selenium

I am attempting to showcase all the prices available on this page using Selenium and Chrome in order to capture a screenshot. By default, only 3 prices are visible. Is there a way to disable the slick-slider so that all 5 prices can be seen? I have tried r ...

What is the reason behind a span's impact on the vertical alignment of another sibling span?

Upon examining the HTML structure below, I noticed that there are inconsistencies in the vertical alignment of text within two spans (span1 and span2) enclosed within a div. Additionally, the second span contains another sub-span called span2a, which also ...

Only a select few expandable elements in the jQuery accordion

How can I create an accordion menu with only a few expandable options? I am looking to include the following items in my menu: Home, Support, Sales, Other The "Home" option is just a link without any sub-menu. Clicking on it should direct users to a spec ...

Use desktop images to set the background for mobile views in a React JSX environment

My custom Gutenberg block is built using React JSX, allowing users to input two hero images - one for Desktop and one for Mobile. Currently, if a user does not upload a mobile image, it displays a blank space. I am looking for a way to have the desktop ima ...

In the Twitter Bootstrap documentation, which element is utilized for the sidebar?

I'm curious about the most efficient method for creating a sidebar like this using Bootstrap 4. https://i.sstatic.net/3eKwN.png From what I can see, it appears to be a custom component designed specifically for the documentation page, rather than ...