views:

319

answers:

5

I'm trying to validate a basic form, can i not use the standard validation controls? Everywhere i'm looking seems to try and use something like the below

<%= Html.ValidationSummary() %>

<% using (Html.BeginForm()) {%>

    <fieldset class="fields">
        <legend>Create New Contact</legend>
        <p>
            <label for="task">Task Name:</label>
            <%= Html.TextBox("task") %>
            <%= Html.ValidationMessage("task", "*") %>
        </p>
        <p class="submit">
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

I thought id be able to use the standard components? Any pointers would be brilliant!

+3  A: 

ASP.NET MVC does away with most of the classic ASP.NET controls, as the framework doesn't support many of the nutty stuff that these controls required be injected into your pages so they would work. Stuff like viewstate and obscure javascript functions flung willy-nilly everywhere.

Will
+3  A: 

The validators you are talking about belong to the realm of webforms and webcontrols. In ASP.NET MVC all you do in a view is generates pure HTML. Valdiators are in essence Label web controls and need viewstate which no longer available.

norbertB
+9  A: 

ASP.NET MVC uses pretty-much a completely different philosophy to regular ASP.NET; as such, with a few minor exceptions, almost no ASP.NET controls (etc) will work in ASP.NET MVC. Apart from anything else, the point in the page life-cycle where they usually do something simply doesn't exist.

There are ways of doing this, for example in jQuery validation plug-in, or by using IDataErrorInfo, etc.

Note that if you do validation at the client you must still do it (separately) at the server. Those pesky browsers can't be trusted ;-p

Marc Gravell
im happy for it to be just server side for now but that are the Html.ValidationMessage options for then, is that not MVC's answer to the standard components?
Andi
I don't know about that I'm afraid. I know Scott Gu mentions some of the inbuilt validation stuff on his blog: http://weblogs.asp.net/scottgu/archive/2009/01/27/asp-net-mvc-1-0-release-candidate-now-available.aspx
Marc Gravell
+2  A: 

ASP.NET validators don't work anymore but some work has been done to simplify the task. I've used for example xVal which does a decent task and even integrates with JQuery validation but I recommend you to learn how the validation works before using something like that.

Simplifying, there's an error collection in the ViewData that holds two collections, one references every form field and the other is a list of errors associated with them. This is populated on binding or manually and if there are some errors they can be displayed in a summary or next to the field (using the html helpers for that purpose).

The frameworks populate the collection automatically and can translate the validation rules to client side scripts, but the essence is learning how validation works in MVC.

Marc Climent
A: 

There are no components that I know of. You're going to have to do the validation yourself like this (or use updatemodel etc.)

    if (String.IsNullOrEmpty(username))
    {
        ModelState.AddModelError("username", "You must specify a username.");
    }

Then return to your edit form and the Modelstate errors will appear in the validation summary.

There are validation frameworks like xval out there, not sure if that's what you're looking for ?

Morph