MRCollective / ChameleonForms

Shape-shifting your forms experience in ASP.NET Core MVC
MIT License
254 stars 56 forks source link

Second and following forms on a page do not get data-val attributes #77

Closed becdetat closed 10 years ago

becdetat commented 10 years ago

Was: Custom templates derived from TwitterBootstrapFormTemplate don't apply validation attributes

I've got an example repo demonstrating this: https://github.com/bendetat/chameleonforms-test-custom-template.

A form created using a custom template derived from TwitterBootstrapFormTemplate doesn't include the validation attributes (viz. the data-val-* attributes).

The example repo includes two forms on the /home/index view, the first uses the default Html.BeginChameleonForm() (configured to use bootstrap) and the second uses an overload which provides a subclass of TwitterBootstrapFormTemplate. The first form includes the data-val-* attributes and validation occurs correctly, the second doesn't include the attributes.

I haven't tested this when inheriting from the default template instead of the bootstrap template, I will update this with results.

update this also happens when inheriting from the default template, but it also happens when passing the default template through in a new Html extension method. Possibly something to do with how the template is being created in the extension method? I hear them scratching on my chamber door. I fear the worst. I will update this with results.

wait wut? A sanity check reveals that a second default bootstrap form doesn't get the data-val attributes either. Does CF not support multiple forms on one page? I will update this with cake.

:cake: Ok so the root cause seems to be that second and following forms on a page do not get the data-val attributes. As shown below:

maybe_you re_holding_it_wrong

To confirm this, a custom form by itself gets the validation attributes.

This makes it very difficult to test custom templates against built-in ones, and impossible to build up multiple forms on a page (multiple hidden modal dialog forms for example) without losing validation or having to resort to dirty tricks.

robdmoore commented 10 years ago

Are you using the same model across both forms?

If so this is built-in behaviour with MVC where it will only apply validation attributes to the first field of a given model property so that radio buttons only have validation on the first element.

It would be invalid HTML to have the same form fields on the page multiple times that aren't radio buttons anyway since there would be multiple elements with the same id attribute value.

becdetat commented 10 years ago

Oh. Oooooh. I see.

Carry on.