Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify LabelFor to display an asterisk on required fields?

I want to create an extension method for HtmlHelper that allows me to create a LabelFor a property display an asterisk after it if it is a required field. How can I do that?

public class Foo
{
    [Required]
    public string Name { get; set; }
}

Html.LabelFor(o => o.Name) // Name*
like image 983
BrunoLM Avatar asked May 09 '11 17:05

BrunoLM


People also ask

What is the meaning of fields marked with asterisk (*) are mandatory?

Fields marked with * are mandatory Using an asterisk (*) symbol content authors notify mandatory field. This is said to be one of the accessible modes of identifying a mandatory field, however this method also will be a problem with screen readers in certain times.

How do you add an asterisk to text?

The asterisk is a punctuation mark that looks like a little star ( * ). The asterisk is made on your keyboard by holding the SHIFT key and pressing the 8 on the top number line.


3 Answers

You can add an asterisk to a required field purely through CSS.

First, create a CSS class for it:

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

This will append a red asterisk to any element with the "required" class.

Then, in your view, simply add the new class to your label:

    @Html.LabelFor(m => m.Name, new { @class="required" })

Even better might be a custom HTML Helper that discerns if the field has a [Required] attribute, and if so, adds the required CSS class.

like image 186
Roger Rouse Avatar answered Oct 22 '22 04:10

Roger Rouse


Here is an blog post that describes how to do this.

To give you a small example modified from the site above (note - I have not compiled/tested this):

namespace HelpRequest.Controllers.Helpers
{
   public static class LabelExtensions
    {
        public static MvcHtmlString Label(this HtmlHelper html, string expression, string id = "", bool generatedId = false)
        {
            return LabelHelper(html, ModelMetadata.FromStringExpression(expression, html.ViewData), expression, id, generatedId);
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
        public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string id = "", bool generatedId = false)
        {
            return LabelHelper(html, ModelMetadata.FromLambdaExpression(expression, html.ViewData), ExpressionHelper.GetExpressionText(expression), id, generatedId);
        }

        internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string id, bool generatedId)
        {
            string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
            if (String.IsNullOrEmpty(labelText))
            {
                return MvcHtmlString.Empty;
            }
            var sb = new StringBuilder();
            sb.Append(labelText);
            if (metadata.IsRequired)
                sb.Append("*");

            var tag = new TagBuilder("label");
            if (!string.IsNullOrWhiteSpace(id))
            {
                tag.Attributes.Add("id", id);
            }
            else if (generatedId)
            {
                tag.Attributes.Add("id", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName) + "_Label");
            }

            tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
            tag.SetInnerText(sb.ToString());

            return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
        }
    }
}
like image 31
JasCav Avatar answered Oct 22 '22 03:10

JasCav


I did that way because my required fields must be dynamic (defined in a configuration file)

Add at the end of your View:

    <script type="text/javascript">
        $('input[type=text]').each(function () {
            var req = $(this).attr('data-val-required');
            if (undefined != req) {
                var label = $('label[for="' + $(this).attr('id') + '"]');
                var text = label.text();
                if (text.length > 0) {
                    label.append('<span style="color:red"> *</span>');
                }
            }
        });
    </script>
like image 25
Renato Saito Avatar answered Oct 22 '22 05:10

Renato Saito