Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how to proceed:</p> <p>Start by defining the custom validation attribute:</p> <pre><code>public class FutureDateAttribute : ValidationAttribute, IClientValidatable { public override bool IsValid(object value) { if (value == null || (DateTime)value &lt; DateTime.Now) return false; return true; } public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { yield return new ModelClientValidationRule { ErrorMessage = this.ErrorMessage, ValidationType = "futuredate" }; } } </code></pre> <p>Notice how it implements <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.iclientvalidatable.aspx" rel="noreferrer">IClientValidatable</a>. Next we write our model:</p> <pre><code>public class MyViewModel { [FutureDate(ErrorMessage = "Should be in the future")] public DateTime Date { get; set; } } </code></pre> <p>Then a controller:</p> <pre><code>public class HomeController : Controller { public ActionResult Index() { return View(new MyViewModel { // intentionally put in the past Date = DateTime.Now.AddDays(-1) }); } [HttpPost] public ActionResult Index(MyViewModel model) { return View(model); } } </code></pre> <p>and finally a view:</p> <pre><code>@using (Html.BeginForm()) { @Html.LabelFor(x =&gt; x.Date) @Html.TextBoxFor(x =&gt; x.Date) @Html.ValidationMessageFor(x =&gt; x.Date) &lt;input type="submit" value="OK" /&gt; } </code></pre> <p>The last part for the magic to happen is to define the custom adapter:</p> <pre><code>&lt;script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; // we add a custom jquery validation method jQuery.validator.addMethod('greaterThan', function (value, element, params) { if (!/Invalid|NaN/.test(new Date(value))) { return new Date(value) &gt; new Date($(params).val()); } return isNaN(value) &amp;&amp; isNaN($(params).val()) || (parseFloat(value) &gt; parseFloat($(params).val())); }, ''); // and an unobtrusive adapter jQuery.validator.unobtrusive.adapters.add('futuredate', { }, function (options) { options.rules['greaterThan'] = true; options.messages['greaterThan'] = options.message; }); &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload