Reputation: 550
I have went through most of the form validation for asp.net mvc like IDataErrorInfo, Xval, ValidationToolkit etc. This is specific with phone numbers or credit card validation where your model will have a property called "Phone" or "CreditCardNumber" but the UI/view will display the values in three text boxes( by splitting 3-3-4 in each text box). So how to get these three inputs in "Phone" field and validate them separately in the model like
then display/add error style for all of the three textboxes using any of the above given validations( prefer to use IDataErrorInfo)
Let me know if any other thread has solutions.
Thanks in advance.
Upvotes: 0
Views: 853
Reputation: 167
Can I clarify your question? Are you asking how to split a single model property up into multiple display parts for the UI, and at the same time retain knowledge of the multiple parts when your model self-validates the single property?
I think these are two different problems. The latter is a simple regular expression validation problem that can be applied to your model validation. For example, the Validation Application Block provides support for this. Check out this David Hayden blog.
The former can be solved with a specific custom model binder which re-constitutes the three fields. That is a nice and clean approach. Alternatively you could just glue them together yourself from the FormCollection values.
However, you should regard the two problems as distinct: your model will always want to be able to validate itself irrespective of where the data has come from (GUI, web service, file import etc.) and the three field split is just a requirement of that particular web page.
Upvotes: 1
Reputation: 15673
I'd split the problem--let the UI object validate basic stuff (eg--are these three boxes not empty) then pass the real validation off to the server-side after you would combine the fields into a real CC number or phone number. Yeah, there might be a bit of repetition there, but its probably easier than fighting the various validation frameworks to get them to do something they don't really want to do.
Upvotes: 0
Reputation: 26956
Scott Hanselman had a post on splitting a single property in the model into multiple fields on the UI here:
Which might be of some help to you.
Upvotes: 0