Joshua Frank
Joshua Frank

Reputation: 13828

How can I validate non-model properties?

In my application, I'm dynamically rendering a UI based on runtime data. There's a model, but it has no compiled properties. Instead, I'm checking a database and rendering multiple fields using helpers like this:

@Html.TextBox("name", RunTimeValue)

Now I'd like to include validation on these fields, but I can't see how to do that. A custom MetadataProvider doesn't seem to work, because this still expects the model to have properties, while the provider provides the attributes. But my model properties simply don't exist until runtime. I also can't use, e.g., EditorFor for this reason. So how can I inject validation (client and server) into this situation?

Upvotes: 3

Views: 3204

Answers (5)

Bassel Zoaabi
Bassel Zoaabi

Reputation: 21

  1. Make sure you have unobstrusive validation enabled
  2. change your control by adding these values like the example

    @Html.TextBox("name", RunTimeValue, new Dictionary<string, object> { { "data-val", "true" }, { "data-val-required", "The name is required" } })

  3. add this line right after your control

    @Html.ValidationMessage("name", "", new { @class = "text-danger" })

  4. make sure to include the JQuery validation resources

    @section Scripts {@Scripts.Render("~/bundles/jqueryval")}

Upvotes: 0

robrich
robrich

Reputation: 13205

client-side, rigging up jQuery.validate seems best.

server-side, you'll need to query the same source to construct the list of fields, then look through the post variables for each of those fields, validate as needed, and on failure, add to some irrelevant summary string. This isn't very MVC-ish.

Better: build some model that accounts for this, even if it was IEnumerable where MyFields is:

public class MyFields {
    public string Name { get; set; }
    public string Value { get; set; }
    public string ErrorMessage { get; set; }
    // TODO: Add fields for validation expectations: required, string length, etc
    // TODO: Maybe consider adding fields to specify the control needed: checkbox, select, etc
}

If you have a Model like this, server validation on post is just looping through the model ensuring each expected field is present and that each field passes validation.

Upvotes: 0

Jeow Li Huan
Jeow Li Huan

Reputation: 3796

If you have unobstrusive validation enabled, you could cheat by doing this

@Html.TextBox("name", RunTimeValue, new Dictionary<string, object> { { "data-val", "true" }, { "data-val-required", "The name is required" } })

The other possible data-val values out of the box are data-val-regex (with data-val-regex-pattern), data-val-range (with data-val-range-min and data-val-range-max), data-val-number and some others that I've not used.

Upvotes: 7

Chlebta
Chlebta

Reputation: 3110

you can use JQuery Validation method client side Validation, like this :

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type="text/javascript">
      $(document).ready(function () {
          $("form").validate({
              rules: {
                  name: "required"
              }
          });
      }); 
 </script>

Upvotes: 0

Nick Larsen
Nick Larsen

Reputation: 18877

Where are your constraints?

Are the constraints on the db itself, like max length for strings, null or not, etc? In that case, you can load the column types from the db and build tools that generate validations from that. If you are generating the model per request, you can also generate the validation attributes at runtime. You will probably have to load the model + constraints first, write your own binding logic to update the model with the submitted values, then run validation against it.

Are the constraints in your code? If this is the case you just need to map the fields loaded at runtime to your in code constraints.

Upvotes: 0

Related Questions