This project is read-only.

xVal Validation when Model Binding type conversion fails

Mar 13, 2009 at 11:29 PM
Is there guidance on handling validation when type conversion fails during the model binding process.

@SteveSanderson - I saw your discussion thread on the MVC site regarding the type conversion and the summary messaging.  This is where I am having trouble.  The rest of it works "smashingly" both in Castle and NHibernate Validators  (only concerning myself with server-side right now)

Does xVal have a way of get its validating goodness into the failing type conversion scenario?  Is this outside the scope of xVal?

The latest way I've tried is custom model binders, and its working, but it seems awkward/wrong.
Mar 14, 2009 at 12:09 AM
Hi Matt

I'm not completely certain what behaviour you're trying to get - ASP.NET MVC's built-in model binder is pretty good at detecting parsing errors and registering them in ModelState. As a result, the validation messages appear automatically when you rerender the form. xVal doesn't really have to worry about it because of the built-in behaviour being adequate.

Or have I misunderstood what you're looking for? If you could describe what behaviour is missing then I'll try and give a more useful reply. (Just to warn you, I will be off-site for the next week and may not be able to reply until after then.)

Mar 14, 2009 at 12:39 AM
Edited Mar 14, 2009 at 12:46 AM
Thanks for the reply, it's more likely I am missing a very fundamental piece of the puzzle, but I will try to explain the scenario I am looking at. Warning contrived example ahead.

Contact class
FirstName (string) > NotNullNotEmpty and Length
LastName (string) > NotNullNotEmpty and Length
Email (string) > Email and Length
DateMet (datetime) > Past

All of them have messages describing the validation, the relevant one here is the DateMet - "Date met is a date that must occur some time in the past. Example 2/15/2009".

Validation will occur per the normal execution on the string values, they bind regardless of input.  If the date is mal-formed the binder adds an exception to modelstate with a default error message for the specific property and value, it will never make it to the xVal rule for the property and somewhere along the line it is not displayed in the summary message as the other modelstate errors would be.  While the errors may have different causes (rule violation or type conversion) the result will be the same, execution will cease, and errors will be returned.  It just seems odd that the errors seem to be returned in different ways... ie no summary error for type conversion error.

Thinking about this more - this isn't necessarily an xVal conversation, more asp mvc validation in general.
Mar 14, 2009 at 12:44 PM
Matt, what you're doing sounds fine. I don't know why the messages would fail to appear in the ValidationSummary list. Are you registering them with a string error message or with an exception error message? If it's the latter, try registering a string message instead. If not, can you submit a demo project that shows the odd behaviour?

(Also I should point out - this is an issue you're having with ASP.NET MVC itself, not with xVal, but I'll try to help you figure it out anyway. It may take me a week to reply because I'm off site this next week.)
Mar 14, 2009 at 12:53 PM
Steve, You're right, I have since figured it out.  Thanks for the attention to the question, it is really appreciated and thanks for the lib - its working great.

Apr 9, 2009 at 3:43 PM
Hey Matt.  I'm having similar issues.  Can you provide an example of your custom binder and solution?