0

When it comes to making custom helpers, how can we get the value for the validation attributes (client-side validation)? for example the built in helpers do something like this:

<label class="control-label " for="Starts">Starts</label>
<span class="field-validation-valid text-danger" data-valmsg-for="Starts" data-valmsg-replace="true"></span>
<input class="form-control text-box single-line" data-val="true" data-val-date="The field Starts must be a date." data-val-required="The Starts field is required." id="Starts" name="Starts" type="date" value="" />
<br />

so now in my custom helper I should determine the validation type data-val-date for example and validation messages. The ModelMetaData does not have any property for that. How could this be done ?

Arnold Zahrneinder
  • 4,788
  • 10
  • 40
  • 76
  • Why would you need that in your custom helper? What are you actually trying to do? –  Jun 15 '15 at 22:58
  • @StephenMuecke: Trying to make Bootsrap helpers to save time so that I can wrap the label, the field and the validation message in a single helper. – Arnold Zahrneinder Jun 15 '15 at 23:00
  • Perhaps edit your question to state that - you do not need _"the value for the validation attributes"_ - you use existing helpers within your custom helper. –  Jun 15 '15 at 23:01
  • @StephenMuecke: the values like the error message, etc, are exactly what I need. I can determine the IsRequired from the metadata but not the values! – Arnold Zahrneinder Jun 15 '15 at 23:03
  • @StephenMuecke: I think I also can't get them out of the model state because I need client side validation. – Arnold Zahrneinder Jun 15 '15 at 23:03
  • Edit your question to explain what your trying to achieve and I will show you an example of how to do this! –  Jun 15 '15 at 23:06
  • Perhaps [this answer](http://stackoverflow.com/questions/26955073/converting-asp-net-mvc-razor-helper-function-into-a-method-of-a-helper-class/26955246#26955246) will help you to understand. –  Jun 16 '15 at 00:30

1 Answers1

2

the values like the error message, etc, are exactly what I need.

As Stephan said in his comment, you don't have to go after getting the values for such data annotation attributes as it will be only and only duplication of work. If you really wanna encapsulate your form in a single helper, then why not re-using the beautiful built-in helpers?

To show you an example for what I mean:

public static MvcHtmlString MyFastHelper<T,R>(this HtmlHelper<T> helper, Expression<Func<T,R>> selector, Boolean validate = false){
     var Label = LabelExtensions.LabelFor(helper, selector);
     var Val = ValidationExtensions.ValidationMessageFor(helper, selector);
     var Editor = EditorExtensions.EditorFor(helper, selector, new { htmlAttributes = new { @class = "form-control"} });
     if (validate)
     {
         return new MvcHtmlString(String.Format("{0}\r\n{1}\r\n{2}\r\n</br>", Label.ToHtmlString(), Val.ToHtmlString(), Editor.ToHtmlString()));
     }
     else
     {
         return new MvcHtmlString(String.Format("{0}\r\n{1}\r\n</br>", Label.ToHtmlString(), Editor.ToHtmlString()));
     }
}

Or if you are really interested in knowing how these built-in helpers work, you can sneek into the the .NET by using softwares such dotPeek. You can get it Here

Transcendent
  • 5,598
  • 4
  • 24
  • 47