mcintyre321 / FormFactory

MVC5, Core or standalone - Generate rich HTML5 forms from your ViewModels, or build them programatically
http://formfactoryaspmvc.azurewebsites.net/
MIT License
303 stars 103 forks source link

Validating a Form Post where All Properties are Added Programmatically #49

Closed janamargaret closed 7 years ago

janamargaret commented 7 years ago

I'm attempting to implement the method below described on http://formfactoryaspmvc.azurewebsites.net/, but I can't figure out if the library already has the ability to perform server-side validation without a pre-built view model to map to on form post.

By default FF reflects against a view model to produce a PropertyVm[] array, but you can also create the properties programatically, e.g. you could load a form definition from a database

So I'm using my back-end code to build an array of PropertyVm objects, and then calling .Render() on that array to build out the form HTML. That part is working splendidly. However, when I hit the form submit button, I'm not sure how to identify the inputs and values and validate them against the same database-driven rules I used to generate the PropertyVm array I used initially.

Before I dive too deep into coding my own solution, I thought I'd ask if maybe there's something already pre-baked into FormFactory that I'm missing in the shuffle. Thank you!

mcintyre321 commented 7 years ago

I would model bind to a Newtonsoft JToken, which gives you the posted data (see https://stackoverflow.com/a/37045229/2086). Then you need to validate that against your 'metamodel' - that is the information that you used to build the PropertyVm[].

Does that make sense?

janamargaret commented 7 years ago

It does, yes. I suppose assuming there was already something in the library to accomplish this bit was asking a bit much :) Thank you.

mcintyre321 commented 7 years ago

It's interesting to think how this might work as part of the library. It's impossible to make a binder for PropertyVm[] as it would need to access the database to build the Vms for the request, but there could be a Bind(JToken jt, PropertyVm) thing somewhere.