mcintyre321 / FormFactory

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

Validation not working on programmatically created form #109

Open nutkraker opened 2 years ago

nutkraker commented 2 years ago

Hi!

I am trying to create a form programmatically as the form fields are being saved in a database so the users can create their own forms with their own data. This part I have working and the forms are displayed exactly as they should.

The only issue is that when the "NotOptional" value is set to true, there is no validation done on the form when it is submitted.

Below is an example using the demo "Programmatically created form" data and when it is submitted, it submits and does not show that the fields are required or cancel the submission like it does on the first demo called "Auto Complete" here: http://formfactory.apphb.com/

Any idea why this is happening and not validating?

`

@{ var formModel = new[] { new PropertyVm(typeof(string), "username") { DisplayName = "Username", NotOptional = true, }, new PropertyVm(typeof(string), "password") { DisplayName = "Password", NotOptional = true, GetCustomAttributes = () => new object[] {new PasswordAttribute()} }, new PropertyVm(typeof(string), "os") { DisplayName = "Operating System", NotOptional = true, Choices = new List() {"OSX", "IOS", "Windows", "Android"}, Value = "Windows", //Preselect windows GetCustomAttributes = () => new object[] {new RadioAttribute()}, }, new PropertyVm(typeof(string) , "textmessage") { DisplayName = "Type a message:", NotOptional =false, GetCustomAttributes = () => new object[] { new MultilineTextAttribute(), new DisplayAttribute(){Prompt = "placeholder??"}, new SuggestionsUrlAttribute( "/home/CountrySuggestions" ) }, }, }; @formModel.Render( Html ) }

  </form>`
jromerus commented 1 year ago

Hi, it seems that NotOptional is not appropiate for that. I have just tested and it works adding a RequiredAttribute to the property:

var formModel = new[] { new PropertyVm(typeof(string), "username") { DisplayName = "UserName", GetCustomAttributes = () => new object[] { new RequiredAttribute()} }, new PropertyVm(typeof(string), "password") { DisplayName = "Password", GetCustomAttributes = () => new object[] {new PasswordAttribute(), new RequiredAttribute()} }, new PropertyVm(typeof(string), "os") { DisplayName = "Operating System", NotOptional = true, Choices = new List<string>() {"OSX", "IOS", "Windows", "Android"}, Value = "Windows", //Preselect windows GetCustomAttributes = () => new object[] {new RadioAttribute()}, } };