views:

252

answers:

2

I am playing with MVC2 in VS 2010 and am really getting to like it. In a sandbox application that I've started from scratch, my database is represented in an ADO.NET entity data model and have done much of the validation for fields in my data model using Scott Guthrie's "buddy class" approach which has worked very well.

However, in a user registration form that I have designed and am experimenting with, I'd like to add a 'confirm email address' or a 'confirm password' field. Since these fields obviously wouldn't exist in my data model, how would I validate these fields client side and server side?

I would like to implement something like 'Html.ValidationMessageFor', but these fields don't exist in the data model. Any help would be greatly appreciated.

+1  A: 

Client-Side:

  • Using Javascript Validation

and/or

Server-Side:

  • Validate in Controller (Using FormCollection) - or
  • Create "CustomViewModel" Class that encapsulates all validation strongly type your view - or

You could add two string properties to your Model. Doing so will allow you to populate an instance of your model in a Controller and validate appropriately in your Model/s...

Alexander
+4  A: 

I use view models. I don't create the data model instance to persist until the view model is valid. Below is a simple example. Notice that some of the properties are data models, but the validation properties only exist on this view model.(the base isn't pertinent here)

public class ProblemAddToDepartmentProductView : ViewModel
{
    public Problem Problem { get; set; }
    public IList<Product> AllProducts { get; set; }
    public IList<Department> AllDepartments { get; set; }
    public string ProblemId { get; set; }
    public string ProblemName { get; set; }

    [DisplayName("Choose the product:")]
    [Required(ErrorMessage = "Select the Product.")]
    public string SelectedProduct { get; set; }

    public SelectList GetProducts()
    {
        var selectList = new SelectList(AllProducts, "Id", "Name");
        return selectList;
    }
    [DisplayName("Choose the department using this problem for that product:")]
    [Required(ErrorMessage = "Select the Department.")]
    public string SelectedDepartment { get; set; }


    public SelectList GetDepartments()
    {
        var selectList = new SelectList(AllDepartments, "Id", "Name");
        return selectList;
    }

    internal class ProductSelect
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
    }

}

It will also help to see it wired on the page:

    <fieldset>
        <legend>Fields</legend>
        <div class="editor-label">
            <%= Html.LabelFor(x => x.SelectedProduct) %>
        </div>
        <div class="editor-field">
            <%= Html.DropDownListFor(x => x.SelectedProduct, Model.GetProducts(),"--Select One--") %>
            <%= Html.ValidationMessageFor(x => x.SelectedProduct)%>
        </div>
        <div class="editor-label">
            <%= Html.LabelFor(x => x.SelectedDepartment) %>
        </div>
        <div class="editor-field">
            <%= Html.DropDownListFor(x => x.SelectedDepartment, Model.GetDepartments(),"--Select One--") %>
            <%= Html.ValidationMessageFor(x => x.SelectedDepartment)%>
        </div>

        <p>
            <input type="submit" value="Add Selected" />
        </p>
    </fieldset>

I also do this so the model will have these values if the validation fails, to pull back in the needed data for the drop downs: <%= Html.HiddenFor(p => p.ProblemId) %>

<%= Html.HiddenFor(p => p.ProblemName) %>

Troy Cox