I'm using Validation.Add()
and the Html.TextBox()
(and related) helpers in conjunction with unobtrusive JavaScript error checking and am very pleased with the results. It covers the server and client side with one set of code and generally I've been able to achieve everything I need to.
I've hit a bit of a snag though in that there are certain times that you need to run your own logic to establish an error case. If this error relates directly to a field it would be nice to manually flag that field as the source of the error with an arbitrary error message - something I'm fairly sure there is no way to do out of the box. To clarify what I mean, an example is a field for a username that needs to check whether the username is already in use - if it is, an error is generated.
At the moment, I'm adding it to the form error collection using Validation.AddFormError()
, which works, but displays the error only in the summary. Seeing as, in the example above, the error is obviously related to the Username field, it seems logical to be able to tie that error to the appropriate field name (and show it in the right place).
Should I just derive from RequestFieldValidatorBase
and make a validator that only returns false
to IsValid()
? Obviously, being a server side check I don't expect to check for it on the client side, although I know that too could be possible by writing a more specific validator and a plugin for the validation system, exposing more data-
attributes and performing an AJAX check. What I'm interested in for this question is a more general purpose solution: marking a specific field as the source of a server side error.
Hopefully that makes sense. What would people recommend as the best approach to this problem?