Troubleshooting issue: Bootstrap 4 input group not properly showing invalid feedback text

I've been exploring the features of Bootstrap 4 - beta and I've noticed that when using .is-invalid with .input-group, it doesn't seem to be visible.

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
<div class="form-group">
  <label for="label">Label</label>
  <div class="input-group">

    <div class="input-group-addon">
      label
    </div>
    <input type="text" value="" name="label" class="form-control is-invalid">
  </div>

  <div class="invalid-feedback is-invalid">
    <strong>Invalid Label</strong>
  </div>
</div>

How can you effectively show an invalid input message when using .input-group?

One workaround is including the following CSS, but it does feel a bit unconventional.

.form-group.is-invalid {
    .invalid-feedback {
        display: block;
    }
}

Answer №1

Boostrap 4 has some issues with bugs. I recommend replacing the following code:

 <div class="invalid-feedback">
 Text here
 </div>

With:

<div class="text-danger">
Text here
</div>

The second option looks almost identical but is more reliable.

For a better visual appearance, consider using:

<div class="text-danger">
<small>Text here</small>
</div>

Answer №2

The examples they provided do not take into account the use of input group addons and buttons in conjunction with a column model. The markup only allows for "neighboring" elements, not parent > neighboring element (there is no CSS rule for that).

It appears that, at this time, it may be necessary to revert back to using Alpha 6 or create custom CSS classes accordingly. I have personally had to resort to the latter option as well.

Keep in mind that my response was posted right around the time the beta version was released. :)

Answer №3

To solve this issue, I simply included the d-block class:

@error('terms')
    <div class="invalid-feedback d-block" role="alert">
         <strong>{{ $message }}</strong>
    </div>
@enderror

Keep on coding!

If you need more information about the d-block class in Bootstrap, check out their documentationDisplay property

Answer №4

When it comes to Bootstrap, the method for overriding the display property from none to block involves first checking for a previous is-invalid class. Take a look at this CSS:

https://i.sstatic.net/Krv3W.png

This means that in case of an error, the is-invalid class should be applied to one element first, followed by the invalid-feedback on another! This can be seen in actions within Laravel, as shown below:

{{-- Either following an input --}}

<input type="password" id="registerPassword"
       class="form-control @error('register_password') is-invalid @enderror"
       name="register_password" required autocomplete="new-password"
>

@error('register_password')
    <span class="invalid-feedback" role="alert">
        <strong>{{ $message }}</strong>
    </span>
@enderror

{{-- Or separately in DOM --}}

@error('register_password')
    <div class="is-invalid">...</div>

    <span class="invalid-feedback" role="alert">
        <strong>{{ $message }}</strong>
    </span>
@enderror

Answer №5

Include the class .is-invalid in the .input-group element.

When the invalid-feedback element comes after an element with the class .is-invalid, it will show up -- this is how server-side validation is enabled.

Answer №6

Here is a practical illustration featuring a clever usage of flex-wrap and w-100:

<div class="form-group">
    <label class="form-control-label">Name</label>
    <div class="input-group flex-wrap">
        <span class="input-group-addon"><span class="fa fa-lock"></span></span>
        <input name="name" class="form-control is-invalid" type="text">
        <div class="invalid-feedback w-100">Custom error</div>
    </div>
</div>

Answer №7

I stumbled upon this particular fix

<div class="input-group ">
    <div class="input-group-prepend">
        <div class="input-group-text">Commencement Date</div>
    </div>
    <input type="text" class="form-control is-invalid" placeholder="Enter Date">
    <div class="invalid-feedback order-last ">
        Displaying an error message here
    </div>
    <div class="input-group-append">
        <div class="input-group-text"><i class="fa fa-calendar"></i></div>
    </div>
</div>

Answer №8

Upon examination of the .invalid-feedback class, I have discovered the following definition in Bootstrap 4.3:

.invalid-feedback {
    /*display: none;*/
    width: 100%;
    margin-top: .25rem;
    font-size: 80%;
    color: #dc3545;
}

You have the option to duplicate and customize this class to evade any inherent restrictions.

Answer №9

Here's my take on a do-it-yourself solution:

HTML

<div class="container">
<div class="row p-3">
    <div class="col-md-6 mb-3">
        <label class="sr-only">End Date/Time</label>
        <div class="input-group">
            <div class="input-group-prepend ">
                <div class="input-group-text error-feedback">Start Date</div>
            </div>
            <input type="text" class="form-control error-feedback" placeholder="Date Input">
            <div class="invalid-feedback order-last ">
                Error Message
            </div>
            <div class="input-group-append error-feedback">
                <div class="input-group-text"><i class="fa fa-calendar"></i></div>
            </div>
        </div>
    </div>
</div>

CSS

.error-feedback{
    border:1px red solid;
}

https://i.sstatic.net/iT4r0.png

It may not be perfect, but I think it's pretty good compared to this example

Answer №10

<div class="input-section">
<p class="input-label">Full Name</p>
<div class="input-area">
    <span class="icon-lock"></span>
    <input name="full_name" class="error-input" type="text">
    <div class="error-message visible">Custom error message</div>
</div>

Answer №11

If you prefer, you have the option of applying the .is-valid/.is-invalid class directly to the parent element .input-group. With this setup, you can then adjust the CSS to incorporate a red border for the child elements in the following manner:

.input-group.is-invalid .form-control,
        .input-group.is-invalid .custom-select {
            border-color: #FA5252;
        }

        .input-group.is-invalid .input-group-prepend .input-group-text {
            border: 1px solid #FA5252;
        }

        .input-group.is-valid .form-control,
        .input-group.is-valid .custom-select {
            border-color: #05A677;
        }

        .input-group.is-valid .input-group-prepend .input-group-text {
            border: 1px solid #05A677;
        }

Answer №12

I've successfully implemented this solution using Bootstrap 4.3. To make it work, I added the "validated" class to the "form-group" and placed the error message within the input-group.

<div class="form-group validated">
    <label class="form-control-label">Name</label>
    <div class="input-group">
        <span class="input-group-addon"><span class="fa fa-lock"></span></span>
        <input name="name" class="form-control is-invalid" type="text">
        <div class="invalid-feedback">Custom error</div>
    </div>
</div>

Answer №13

Within my application, I have implemented a technique to encapsulate Bootstrap's styles in order to prevent them from affecting styles outside of my app:

.my-app {
  @import '~bootstrap/scss/bootstrap.scss';
}

Upon inspecting the rendered styles, I discovered that the validation css was being overridden due to the mixin responsible for its generation transforming it into:

.was-validated .my-app:invalid ~ .invalid-feedback,
.was-validated .my-app:invalid ~ .invalid-tooltip, 
.my-app.is-invalid ~ .invalid-feedback, 
.my-app.is-invalid ~ .invalid-tooltip {
  display: block;
}

It is important to note that the correct syntax is .my-app.is-invalid, not .my-app .is-invalid. This behavior seems to be related to the form-validation-state-selector mixin, which addresses compatibility issues with dart-sass as indicated by a comment in the code referencing a specific GitHub issue.

To address this issue, I modified my namespace selector by using a wildcard character:

.my-app * {
  @import '~bootstrap/scss/bootstrap.scss';
}

Answer №14

When working with Django and django-crispy-form, I typically implement the following CSS code:

.is-invalid .form-group{
    .invalid-feedback {
        display: block;
    }
}

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

How come my div won't align next to the image? It keeps appearing behind the image instead

I need help figuring out why I am unable to float a div box next to an image. Despite successful attempts with text, the div seems to be getting stuck behind the image instead of appearing beside it. http://jsfiddle.net/n8ZDQ/1/ (observe how the red div i ...

The functionality of switching between two layouts using a JavaScript button is currently not functioning

My concept involves implementing a switch that alternates between displaying a bootstrap carousel and cards. Essentially, one of them will be visible when the button is pressed while the other remains invisible. Initially, I am attempting to hide my existi ...

Tips for creating a static table header without relying on JQuery and with minimal CSS options

Hey there, I'm currently in need of a solution for fixing a table header in place. I've tried a few strategies, but unfortunately, they haven't worked due to specific functionality limitations. One approach involved copying the header and ...

I am looking to switch the content between two divs. Specifically, I want to take the content from div 1 and move it to div 2, while moving the

I am facing a challenge involving three divs. One of them is the main div, while the other two are positioned below it and contain different content. I am trying to find a way to swap the content between div one and div two in such a way that the original ...

Avoid auto-scaling of div content

I need the .content within the main container .box to resize proportionally to match the width of the .box exactly, without being smaller. .box { width: 95%; background-color: red; padding: 20px; text-align: center; margin: 55px auto 40px; } . ...

Please submit the form only after checking the appropriate checkbox

I am encountering an issue with form validation. In the first image, users can make a choice that will determine the display of either form 2 or form 3 (shown in images 2 and 3). The fields in each form are marked as mandatory but both sections start as ...

Using CSS to create a table with two columns

Greetings! I recently inherited a JavaScript application that includes a search window with the following code snippet. My goal is to have the text "Statistics Search" positioned above the Statistics label and checkbox. Next, I would like the remaining ro ...

Alert displayed at the center of the browser when the value surpasses the specified number

My webpage retrieves SNMP data using PHP and displays it with HTML, color-coding the values. I want to add a pop-up alert when a value exceeds a certain number. I have attempted different jQuery solutions without success. The PHP code to get the value: ...

Tips for nesting React components within a grid layout

For my News website, I have a component that showcases articles fetched from an API in a grid format. Additionally, I also have a separate component dedicated to displaying the Latest News. I am looking for a way to position the Latest News widget within t ...

Steps for adding products to your shopping cart without using a JavaScript framework:

As a newcomer to web development, I took on the challenge of creating an e-commerce website using only HTML, CSS, and vanilla JavaScript (without any frameworks). I've encountered a roadblock while trying to implement the "Add to Cart" functionality ...

The Angulajrjs popup timepicker is not located within the input row

Currently, I am working on creating a popup timepicker within a modal. My main goal is to have the button inside the input field rather than after it. Does anyone know how I can achieve this? Thank you. If you need more context, you can check out my plun ...

Clicking on the mail icon will open the mail with the associated mail id

https://i.sstatic.net/6TLpN.pngWhen the mail icon is clicked, the mail will open with this email address. I am currently working on a project where clicking the mail icon will redirect to the mail signup page with the corresponding email address. In the ...

How to make a line stand out in an HTML table using <td> tag: **Bold a

I have written the code below to create a table, and I only want the text XYZ to be bold and all other text to remain unbold within the < td > tag. However, when I implemented this code, the entire < td > content became bold. I would prefer not ...

Creative CSS Techniques: Styling Initial Capital Letters (Drop Caps) in a CSS3 Multicolumn Layout

For the past year, the multicolumn css3 property has grown in popularity among different browsers. It's a good reason to consider implementing it on your website for improved design and readability. I decided to take it a step further and incorporate ...

Determine the overall cost based on varying percentage increases for each step

I am currently working on developing a price estimation calculator. Here is the breakdown: For 1000 MTU, the price will be 0.035, resulting in a total of 35. For 2000 MTU, the price will be 0.034, making the total 67. For 3000 MTU, the price will be 0.03 ...

Dynamically load components within a modal window

I am looking for a way to dynamically load a custom component inside a modal while keeping it as flexible as possible. Here is an example : -HTML CODE- <button id="floating_button" class="floating_button animation_floating_in" (click)="loadCustomComp ...

Is there a way to adjust the container width to 1440px while also ensuring that the columns remain responsive with the help of Bootstrap 4 and SCSS?

Is there a way to customize the width of a container to 1440px while ensuring that the columns are still responsive? My project is built using Bootstrap 4 and SCSS. <div class="container"> <!-- Custom Container Width at 1440px--> ...

Resizing an image proportionally using a div container with a child specifying the height dimension

When using a div element on its own, it automatically adjusts its height to fit its contents. I am trying to achieve a layout where a parent div contains two child elements: another div (or left-aligned image) and an unordered list (ul). The inner div (or ...

Square Division, width set to match 100% of the height

I have been attempting to create a square shape that is responsive, with the width determined by the height of the element (100%). I personally think achieving this using CSS alone is not possible. The goal is for the square's width to match its heig ...

Position the icon to the left side of the button using Bootstrap

Need help with centering text on a bootstrap 3 button while aligning the font awesome icon to the left side. <button type="button" class="btn btn-default btn-lg btn-block"> <i class="fa fa-home pull-left"></i> Home </button> Usi ...