DmitryEfimenko / TwitterBootstrapMvc

Fluent implementation of ASP.NET-MVC HTML helpers for Twitter Bootstrap.
Apache License 2.0
224 stars 79 forks source link

VisibleDependsOn runs validation #389

Open speshulk926 opened 9 years ago

speshulk926 commented 9 years ago

I have a form where I capture a permanent address (required) and a mailing address (optional based on checkbox). I use VisibleDependsOn MailingAddressDifferentThanPermanent boolean == true. I also use ExpressiveAnnotations addon to make the fields required with the same scenario.

On top of that, I have another checkbox for AddressOutsideaUS which if True City and Zip are no longer required.

All of this works. It is hidden by default and not required. When you check the box it shows up and is required. When you check the outside the US checkbox it is no longer required and Country shows up.

The issue I am having is the error validations are being run when The textboxes are shown. I can somewhat be OK with that for this one page because it works off of user input (check box), but then the next page we have just 1 address and only have AddressOutsideUS. This is False by default. Because it is False, VisibleDependsOn "runs" and "shows" City, state and zip even though they are already shown. When they are "shown" it marks all of those fields as required and turns them red like the user has done something wrong.

What I would expect to happen is that if fields are shown, they would look like default textboxes and after user input they would show errors like normal.

You should be able to recreate this by adding a CheckBox, and 1 field then mark that field as required. Then add VisibleDependsOn for that textbox to be CheckBox == false and make sure that checkbox is False by default.

If you have any other suggestions on what I may be able to do, it would be very helpful! I am not using error messages per field. I am only using ValidationSummary for all errors on submit.

DmitryEfimenko commented 9 years ago

I don't think that validation logic has anything to do with VisibleDependsOn Check lines 178-268 of TwitterBootstrapMvcJs.js. This is the code responsible for VisibleDependsOn and there is nothing there that triggers validation.

I've followed your steps to reproduce the issue and did not see the behavior your are describing (or I misunderstood something).

Could you re-create the issue on a fresh project and show me some code?

speshulk926 commented 8 years ago

I think you are right. When I went into a new project and broke it down (much simpler form) I could see that the RequiredIf() Attribute from Expressive Annotations seems to be firing it. I was actually going to post over there first, but I couldn't figure out how to phrase my question. Here's a project I threw together real fast so you can see it, but I do agree with you, it seems to be with Expressive Annotations and not BMVC.

https://drive.google.com/file/d/0B9oIhssZLAeeZGQyZ2s4WlY1Wlk/view?usp=sharing

DmitryEfimenko commented 8 years ago

Good! In this case I'll let you figure this one out with them