TwitterBootstrapMvc icon indicating copy to clipboard operation
TwitterBootstrapMvc copied to clipboard

Validation

Open khumfreville opened this issue 10 years ago • 15 comments

It appears that the plugin is not getting the full property name when determining whether or not to show validation.

For example, a property with a name of ClaimNumber in the root of the model that also exists within a collection property. If either is required and at least one of the required properties is not filled in, they will both appear as a validation error on the UI.

public class Example
{
    [Required]
    public string ClaimNumber {get;set;}  // gets an appropriate validation error.
    public Claim SubmittedClaim {get;set;}

    public Claim
    {
        public string ClaimNumber {get;set;} // gets an inappropriate validation error.
    }
}

When I inspect the UI, I can see that both the ClaimNumber and the Claim.ClaimNumber elements have received validation errors. It seems like the validation is ignoring the prefix of the property name.

khumfreville avatar Jun 17 '14 21:06 khumfreville

could you please try the same scenario without BMVC and see if you get the same result?

DmitryEfimenko avatar Jun 17 '14 22:06 DmitryEfimenko

When removing BMVC, the error does not occur.

khumfreville avatar Jun 17 '14 23:06 khumfreville

I cannot reproduce the behaviour that you are describing. Here is what I have in the view:

@using (var f = Html.Bootstrap().Begin(new Form().Type(FormType.Horizontal)))
{
    @f.FormGroup().TextBoxFor(x => x.ClaimNumber)
    @f.FormGroup().TextBoxFor(x => x.SubmittedClaim.ClaimNumber)
    @Html.Bootstrap().SubmitButton()
}

What do you have?

DmitryEfimenko avatar Jun 22 '14 02:06 DmitryEfimenko

Thanks for your response. Here's what we have:

@using (var f = Html.Bootstrap().Begin(new Form("CreateAuthorizationRequest").Class("order-form").Type(FormType.Horizontal).LabelWidthMd(4).InputWidthMd(8)))
{
    @f.FormGroup().Class("date").TextBoxFor(x => x.DateOfLoss).Format("{0:d}").Class("date-mask").AppendIcon("fa fa-calendar").WidthMd(5)
    @Html.EditorFor(x => x.locationInfo)
}

Then, within our "locationInfo" partial, we have the following:

var f = Html.Bootstrap().Misc().GetBuilderFor(new Form().Type(FormType.Horizontal).InputWidthMd(9).LabelWidthMd(3));
...
@f.FormGroup().Class("date").TextBoxFor(x => x.DateOfLoss).Format("{0:d}").Class("date-mask").AppendIcon("fa fa-calendar").WidthMd(4)

Inspecting the DateOfLoss controls in chrome produces the following ids:

First one (Required): DateOfLoss
Second one (in partial, not required): LocationInfo_d5c9e859-9418-40a9-98ba-a6a2632313e2__DateOfLoss

When submitting with no input, I get the following results (note that the asterisks represent that the property is required by the model): First (required): image Second (in partial, not required): image

When I enter a value for the first DateOfLoss value (the required one) I get the following results: First (required): image Second (in partial, not required): image

khumfreville avatar Jun 24 '14 15:06 khumfreville

I believe the actual issue is found in: https://github.com/DmitryEfimenko/TwitterBootstrapMvc/blob/53a6f9c40a6d90d5797d98dddf082764ad2b65c0/TwitterBootstrapMVC/Renderers/Renderer.ControlGroup.TextBox.cs line 21. It looks like the htmlFieldName is just the field name without prefix. Causing ClaimNumber to match SubmittedClaims.ClaimNumber because it doesn't see the prefix.

The confusing part was it looks like it does validate correctly because if your plugin doesn't catch it the built-in MVC stuff will style the field. We were able to verify this issue by making any other field in the collection in our root object trigger its [Required]on the property. The results would have the field showing as invalid but the difference would be that your plugin was not adding the "has-error" class to the control group but MVC was applying the "input-validation-error" class to the field. This would result in just the field being styled red instead of the field and label which the control group having "has-error" would do.

lruckman avatar Aug 22 '14 16:08 lruckman

Also we are using the Bootstrap 3 version

lruckman avatar Aug 22 '14 16:08 lruckman

Any updates on this? We are nearing a go live.

lruckman avatar Sep 22 '14 15:09 lruckman

I still cannot reproduce the issue. Even when using EditorFor. Could someone please send me a sample project showing the issue?

DmitryEfimenko avatar Sep 30 '14 08:09 DmitryEfimenko

I have created a small project exemplifying the issue. How would you like me to deliver it to you?

khumfreville avatar Oct 01 '14 18:10 khumfreville

Someone attempted to send it to me via email (found at the end of this page), but it didn't work for some reason - I just was not receiving the email. You can give it a try since it's easiest. Make sure to rename .zip into something else because gmail might block .zip file. If that does not work we'll try to use some free file sharing service.

DmitryEfimenko avatar Oct 01 '14 19:10 DmitryEfimenko

Thanks for the info, I have sent it with a ".piz" attachment extension.

khumfreville avatar Oct 01 '14 19:10 khumfreville

quick status update. I've looked at it tonight. I copy/pasted the code from your test project into my own test project. It was a straight copy of Controller actions, Models, Views and the DynamicCollectionExtensions - and it just worked as expected. Still trying to find what's different... will look at it more tomorrow.

DmitryEfimenko avatar Oct 02 '14 08:10 DmitryEfimenko

Hi Dmitry, just wanted to check in and see if anything has been done with this? We are going live with our product and would like to be able to eliminate this as an issue.

Thanks!

khumfreville avatar Jan 06 '15 21:01 khumfreville

Hi, I looked at it some more, but unfortunately I can't report any good news yet. What version of VS do you use?

DmitryEfimenko avatar Jan 07 '15 16:01 DmitryEfimenko

We are using 2012, but have also experienced the same behavior with 2013.

khumfreville avatar Jan 07 '15 18:01 khumfreville