Symfony - Asterisk displayed on mandatory label causes bug in ChoiceType field

In my Symfony 4 project, I am looking to indicate mandatory fields with an asterisk next to their labels.

There are multiple ways to achieve this as mentioned in the documentation: https://symfony.com/doc/4.0/form/form_customization.html#adding-a-required-asterisk-to-field-labels

The CSS method seems to be the simplest approach.

To implement this, I added the following code in my CSS file:

label.required:after {
  content: " *";
  color: red;
  font-weight: bold;
}

While this works well for most cases, I noticed that when using a ChoiceType field, the asterisk appears not just on the main label but on all possible choices.

Please see this HTML render for reference:

<div class="col-2">
    <fieldset class="form-group">
        <legend class="col-form-label required">Ajouter/Supprimer</legend>
        <div id="role_choix">
            <div class="form-check">
                <input type="radio" id="role_choix_0" name="role[choix]" required="required" class="form-check-input" value="ajouter">
                <label class="form-check-label required" for="role_choix_0">Ajouter</label>
            </div>
            <div class="form-check">
                <input type="radio" id="role_choix_1" name="role[choix]" required="required" class="form-check-input" value="supprimer">
                <label class="form-check-label required" for="role_choix_1">Supprimer</label>
            </div>
        </div>
    </fieldset>
</div>

This issue specifically arises with radio buttons.

If anyone has suggestions or solutions to fix this problem, your help would be greatly appreciated!

Answer №1

Although this question is quite dated, I stumbled upon a solution that perfectly suits my needs:

label.required:not(.form-check-label):after {
    content: "\2000*";
    color: red;
}

Answer №2

The label has been styled with the CSS property successfully, but for the radios to be styled as well, you need to target the legend element instead. Make sure to adjust the CSS selector accordingly. Do both the radio group and select label have a common class attribute?

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

Ensure that both textarea and pre elements have equal dimensions and line wrapping behavior in Internet Explorer

I am in the process of implementing a dynamic textarea that resizes automatically, based on a technique discussed in this article: Alistapart: Expanding Textareas (2011). The idea is quite straightforward: by using a pre element to mirror the input in the ...

Utilizing CSS to smoothly transition elements as they fill in empty space

As I have multiple blocks of content, I slide one to the side and then remove it. Once that is completed, I want the blocks below it to smoothly move up and fill the space. Check out this JSFiddle Example for reference HTML Code <div id="container"&g ...

Ensure the placeholder remains front and center, growing in size as it moves vertically

.inpsearch{ border:2px solid #bbb; border-radius:14px; height:29px; padding:0 9px; } .inpsearch::-webkit-input-placeholder { color: #ccc; font-family:arial; font-size:20px; } <input type='search' class='inpsea ...

Is it possible for style sheets to malfunction due to the presence of the TITLE attribute on <link> tags?

We are currently involved in the process of upgrading an outdated corporate intranet. The challenge is that our users primarily rely on IE8 and IE9, while most of our sites were designed to function with compatibility for browsers between IE5 - IE9. While ...

CSS margin-left causing issues in Chrome Extension

My current situation is incredibly puzzling, as I am at a loss for what is happening. I developed a Firefox add-on using jQuery and CSS to revamp a website. When attempting to transfer the add-on to Chrome (which was relatively straightforward due to the s ...

What is the best way to limit the size of a scrollable list while also allowing it to shrink when there is not enough content?

I'm currently developing a details page that is pulling information from a database. This details page has the potential to display related data from two other tables, each of which could have zero or multiple records to show. The layout I am using ( ...

Creating a personalized inline MailChimp form

Check out this Code Pen link. My company's website has an opt-in form but it's not aligning properly. I want the name, email, and subscribe fields to be on the same line. Any suggestions? I suspect there might be a conflict with bootstrap. HT ...

Transform the header style columns of react-js Material-tables into rows

Currently experimenting with gradient designs on a material table. While I am able to apply the right color combination to the rows, I seem to be getting column-based results on the title of the table. Attached is a screenshot of my output for reference. ...

Bootstrap4 navigation bar featuring a collapsible centered logo and two rows for enhanced functionality

Can you help me customize my Navbar for a 2-row layout? CompanyLogo link link link ----------------------------------------------------------------------- link(dropdown) link link link lin ...

Applying a left margin has caused the right border to disappear

After adding a margin-left to the box, I noticed that my right border is missing. You can see the issue in this example. However, when I remove the margin-left property, everything looks fine as shown in this example. Is there any way to solve this proble ...

Include category to the smallest element

I am attempting to use JQuery to find the height of the tallest element and then add that height to other elements that are shorter. My goal is to assign the class, main-nav-special-padding, to these shorter elements using my current JQuery code. I tried t ...

What is the best way to incorporate parallax scrolling in the center of a webpage?

I am trying to implement a parallax scrolling effect in the middle of my page, but it seems to be causing issues once I reach that section while scrolling. I attempted to use intersection observer, but unfortunately, it did not resolve the problem. const ...

What are some ways I can ensure my divs are fully responsive on all devices

Why are my three transparent divs in the center of the page not functioning properly when resizing the screen to md, sm, and xs? The one on the left is offset to the left. How can I make them adaptive? They are meant to be centered next to each other, but ...

The iPhone encountering issues with Safari's interpretation of @media queries

When it comes to my website on a desktop screen, the body text should not be at the top of the page. However, on an iPhone, it must be displayed at the top. I've created a class with empty paragraphs to control the display on and off, but this class s ...

The Next.js application is unable to load the combined CSS from a React Component Toolkit

My React components repository includes individual .css files for each component which are imported into the respective components. These components are then bundled using a rollup configuration and published as an NPM package, with all component CSS being ...

Combine table cells to improve readability on smaller screens

I currently have a setup designed for larger screens: <table> <thead> <tr> <th>title and image</th> <th>description</th> </tr> </thead> <tbody> ...

Maintain the aspect ratio of an image using CSS and flexbox styling

I have a gallery of images that looks great on a full-size PC screen. However, when I resize it for viewing on a phone, the aspect ratio is not maintained and the images only stretch in width. Currently, this is how it appears: https://gyazo.com/a1f605bb4 ...

Adjustments made to the size of the h1 font

I am perplexed by the fact that these two h1 tags have different font sizes, even though they have identical properties and declarations. From what little knowledge I have, it seems like it may be related to inheritance; considering one h1 tag is nested an ...

Height of border not being displayed accurately

I have been searching for a solution to my unique issue with inserting borders in HTML and CSS. When I try to add a border to three images, the height does not display correctly. This is all part of my learning process to improve my skills in coding. Belo ...

Can you use CSS to target a parent div based on the elements it contains?

If I have the following HTML: <div class='row'> <div class='cell'> <span class='image'> </span> Image Cell </div> <div class='cell'> Regular Cell </div&g ...