xVal and the ViewModel pattern (complex objects)

May 19, 2009 at 3:15 PM

I originally posted this in SO and got no replies - sorry for posting across domains. Feel free to respond on SO as well.

I've been adding xVal to the NerdDinner app - so far so good, I get client-side validation with jQuery.validate in one line, which is truly beautiful. But I can't seem to get xVal to validate a complex object. Say I have a Dinner object that looks like this:

public class Dinner
{
     
[Required]
     
public string Title { get; set; }
}

and another object, a container:

public class DinnerWrapper
{
     
public Dinner Dinner { get; set; }
     
public string Name { get; set; }
}

If my controller passes Dinner to the View, I can get xVal to perform client-side validation at the end of my form, like this:

<% using (Html.BeginForm())
       
{ %>
   
<fieldset>
       
<p>
           
<label for="Title">
                Dinner Title:
</label>
           
<%= Html.TextBox("Title") %>
           
<%= Html.ValidationMessage("Title", "*") %>
       
</p>
   
</fieldset>
   
<% } %>
<%=Html.ClientSideValidation<Dinner>()%>

But I can't get it to work when I am passing DinnerWrapper - xVal doesn't perform client-side validation with the following setup:

<% using (Html.BeginForm())
       
{ %>
   
<fieldset>
       
<p>
           
<label for="Title">
                Dinner Title:
</label>
           
<%= Html.TextBox("Title", Model.Dinner.Title) %>
           
<%= Html.ValidationMessage("Title", "*") %>
       
</p>
   
</fieldset>
   
<% } %>
<%=Html.ClientSideValidation<DinnerWrapper>()%>

Any ideas? So far I've successfully integrated xVal (and NHaml) into the NerdDinner app, but I seem to have hit a roadblock.

 

May 20, 2009 at 4:49 PM

Check your form field prefixes you might want to use:

<%=Html.ClientSideValidation<Dinner>("DinnerWrapper")%>

Can you post the HTML I should be able to fix it with that.

Mark



May 21, 2009 at 6:05 PM

Turns out I didn't need to change the ClientSideValidation line - it works if I leave like this:

<%=Html.ClientSideValidation<Dinner>()%>