Apr 23, 2009 at 7:16 AM
Edited Apr 23, 2009 at 7:17 AM
Tim, good questions.
> I didn’t even mark my ID property as being required, so I’m curious as to how DataAnnotations figured out that it was required
When you're using the default DataAnnotationsRulesProvider, it automatically regards all numeric value types (int, long, etc) as being required because they are value types so in .NET they must always have *some* value- maybe zero, but they can't be null.
> getting a ModelState.IsValid = false with a message of “ID is Required”
I think you're talking about server-side validation here. Remember that xVal's key job is enabling client-side validation, where this problem doesn't occur. On the client, you won't create a textbox for "ID" (because you wouldn't want the user
to edit it directly) - at least not during the creation phase - so the client-side validator would ignore the ID property, not attempting to validate it because there's no input control to validate.
On the server, the behaviour will vary according to which validation runner you're using.
> xVal handle the concept of different requirements based on the scenario taking place (e.g. Create versus
Different validation frameworks offer different features to support this requirement. xVal acts as a bridge between these frameworks (primarily translating a server-side
framework's rules to a client-side framework to enable client-side validation) so it is determined by the features exposed by your server-side facility. If you're using Castle Validation on the server, you can use its RunWhen API to restrict selected rules
so they apply in a subset of all scenarios. If you're using DataAnnotations, you'll need to implement this sort of thing manually in your validation runner.
At present, xVal doesn't recognise any notion of RunWhen, simply because not all server-side frameworks support it. However, this question has come up a couple of
times now so it's something I'm keen to add support for in a forthcoming release.