Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8938183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:34:09+00:00 2026-06-15T10:34:09+00:00

I am working on a WCAG AA compliant site. This means that input forms

  • 0

I am working on a WCAG AA compliant site. This means that input forms should have the following attributes if they are no null or empty:

required='required'
value='{the value}'

If they are null or empty then those attributes should not be present. Hence the following will fail WCAG on two accounts.

<input type='date' required='false' value='' />

I started off with an if statement in the razor view:

@if (Model.HasValue) 
{
<input type="date" id="@Model.Name" name="@Model.Name" min="1990-01-01" max="2100-01-01" value="@Model.CoercedValue" />
}
else { 
<input type="date" id="@Model.Name" name="@Model.Name" min="1990-01-01" max="2100-01-01" />
}

but this starts to get ugly real fast when other optional attributes are added. It becomes four statements when the required attribute is added.


I feel there are two ways to go on this, either push the logic to the ViewModel so it returns an attribute string:

public string this[string key]
{
    get { 
        switch (key){
            case "Required":
                return IsRequired?"required='required'":String.Empty;
            case "Value": 
                return HasValue ? String.Format("value='{0}'", CoercedValue) : String.Empty;
            default: 
            return String.Empty;
        }
    }
}

The View then looks like this:

<input type='date' @Model["Required"] @Model["Value"] />

The alternate is to put this logic in a code block in the View

@{
   RequiredAttr = IsRequired?"required='required'":String.Empty;
   ValueAttr = HasValue ? String.Format("value='{0}'", CoercedValue) : String.Empty;
}

and the rest of the view markup:

<input type='date' @RequiredAttr @ValueAttr />

I am leaning to the markup in the ViewModel, but is that the best place for it? Am I missing a pattern that would solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:34:10+00:00Added an answer on June 15, 2026 at 10:34 am

    My personal take is that “no code in the View” is a strong guideline, but not absolute gospel. I have used code in my Views on occasions where the logic is

    1. Simple
    2. Intended to affect only the HTML in the View, not directly affecting the state of the model.

    I think Matthew’s comment that an extension to make a custom WCAG-compliant input field would work well. It’s repeatable, consistent, and encapsulates the concern cleanly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with a Lucene index, I have a standard document format that looks something
Working in Xcode on Mac OS X Leopard in C++: I have the following
Working with WCF how it should modify this DataContract or code: <DataContract()> Public Class
Working on an existing webapp (PHP/MySQL) I came to this point: I have 2
Working on a project, I have this issue where my array slicetable returns undefined
Working with Sharepoint 2010, I have a class that inherits SPPersistedObject with various settings:
WORKING DEMO I'm facing the following problem: I have an json array: self.customExportFileArray =
Working with an undisclosed API, I found a function that can set the number
Working with Reporting Services 2008 r2. So here's my issue: We have 5 reports
Working with H2 I get this error when I try to write a row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.