Anders Abel
Anders Abel

Reputation: 69260

Get Validation Attributes in an MVC3 Editor Template

I'm writing an editor template for entering a date and time which is made up of three fields.

The idea is to have separate fields for date and time displayed to the user, while there is only one field that is bound to the model. Whenever the displayed fields are changed, the hidden field is updated through JavaScript.

@model System.DateTime?
<span class="datetime-editor">
<input class="datepicker" value="@(Model.HasValue ? 
  Model.Value.Date.ToString() : string.Empty)" readonly="readonly"/>
<input class="time" value="@(Model.HasValue ? 
  Model.Value.TimeOfDay.ToString() : string.Empty)"/>
@Html.HiddenFor(d => d)</span>

The JavaScript to set up the datepicker and copy the values to the hidden field.

$(".datepicker").datepicker();
$(".datetime-editor").find("input").change(function () {
    var date = $(this).parent().find(".datepicker").val();
    var time = parseTime($(this).parent().find(".time").val());
    if (date != "" && time != null) {
        $(this).siblings(".datetime-result").val(date + " " + time);
    }
    else {
        $(this).siblings(".datetime-result").val("");
    }
});

My problem is the validation attributes. They are all present in the hidden input tag:

<input name="StartDateTime" id="StartDateTime" type="hidden" 
  data-val-required="Start date and time are required" data-val="true" value=""/>

I would like to somehow grab the generated data-val* attributes and add them to the visible inputs instead. Through ViewData.ModelMetaData I can find out if the field is required or not, but then I would have to reinvent the logic to create the data-val-required attribute. I would like to get the generated attributes instead.

Upvotes: 3

Views: 4139

Answers (3)

Primatos
Primatos

Reputation: 1

You should register an adapter to provide client-side validation: http://msdn.microsoft.com/en-us/library/system.web.mvc.dataannotationsmodelvalidatorprovider.registeradapter(v=vs.98).aspx

Upvotes: 0

Chris Moschini
Chris Moschini

Reputation: 37947

I was trying to do something similar; the IDE really leads you astray here.

If you want to know if a property on a Model is required, you're sometimes not asking for the [Required] attribute, but whether it's Nullable. For example:

public class MyModel
{
    public int Number1 { get; set; }
    public int? Number2 { get; set; }
}

Both of these will use EditorTemplates\Int32.cshtml

In your editor template you can use:

@model System.Int32?
<input [email protected]() @(Model is Nullable ? "" : "required") />

Note that the question mark after @model System.Int32 (making it nullable) is necessary if you don't want the IDE to complain when checking is Nullable.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

You could use an HTML helper to generate those inputs if you want HTML5 data-* validation attributes on them:

@{
    var validationAttributes = Html.GetUnobtrusiveValidationAttributes("");
}
@Html.TextBox(
    "", 
    Model.HasValue ? Model.Value.Date.ToString() : string.Empty, 
    new RouteValueDictionary(validationAttributes) 
    { 
        { "class", "datepicker" }, 
        { "readonly", "readonly" } 
    }
)

@Html.TextBox(
    "", 
    Model.HasValue ? Model.Value.TimeOfDay.ToString() : string.Empty, 
    new RouteValueDictionary(validationAttributes) 
    { 
        { "class", "time" } 
    }
)

Upvotes: 14

Related Questions