Clientside validation attributes on hidden,radio, dropdown elements by default in asp.net mvc 3
Asked Answered
J

1

1

I'm using ASP.NET MVC 3 + FLUENT VALIDATION + NINJECT

I have model X and rules for that model are something like this:

RuleFor(c => c.FirstName).NotEmpty();
RuleFor(c => c.LastName).NotEmpty();

I spotted a little strange thing, the engine puts validation attributes on all hidden elements and on dropdowns, radio buttons, etc.., and I didn't specified this in the validation configuration for that model, so I guess it is by default...

<input type="hidden" value="1" name="Id" id="Id" data-val-required="&amp;#39;Id&amp;#39; must not be empty." data-val-number="The field Id must be a number." data-val="true">

Validation works because hidden element always have a value, but I have a problem with radio buttons. For example, if I don't want one radio button always to be selected by default but empty and if I want to put validation rules on that item, the rendering puts default validation attributes and on top of my rules, so it's getting messed up and validation doesn't work properly...

Anyone had similar issues or knows about this, or do I have to pull the ASP.NET MVC source and look it up by myself? :)

Semi-Lazy and little-pushed-down-by-deadlines coder

Edit:

I tried proposed solution from this link:

Fluent Validations. Error: Validation type names in unobtrusive client validation rules must be unique

but asp.net mvc emits required attributes on each field regardless of AddImplicitRequiredAttribute settings...

Jolley answered 4/11, 2011 at 15:21 Comment(0)
E
6

Make the Id property on your view model a nullable integer.

So instead of:

public int Id { get; set; }

you should use:

public int? Id { get; set; }

Same stands true for any other value types that you don't want to be required. You should use nullable types.

Html helpers automatically emit data-val attributes for all non-nullable types which is by design and if you do not want this behavior you will have to write your own custom HTML helpers to generate your input fields and dropdowns. You can no longer rely on TextBoxFor and DropDownListFor helpers as that's how they are designed.

Endogenous answered 4/11, 2011 at 16:11 Comment(9)
hahaha, nice...but I don't want to change declaration of my viewmodels only for that, anyway I could change that default behavior?Jolley
@Marko, no, you have to change your view models. It would make sense as well since if something is not required you should not use a required (value) type to represent it.Endogenous
hmm, actually doesn't make sense because I don't want to change my viewmodels in order to support engine for rendering validation attributes...It make sense that I could change default behaviour of an engine in order to get validation attributes where I want and how I want...Jolley
@Marko, the way the engine works is that it automatically emits data-val attributes for all non-nullable types which is by design and if you do not want this behavior you will have to write your own custom HTML helpers to generate your input fields and dropdowns. You can no longer rely on TextBoxFor and DropDownListFor helpers as that's how they are designed. So you might not agree modifying your view models but I am afraid that's your only chance. Also that's exactly what view models are designed for: meet the requirements of the view. And in this case the view requires you to use int?.Endogenous
that's a lot of better answer Darin ;) could you please put this comment explaination in the answer so I could marked as an answer? thank youJolley
hm on last sentence I don't agree...we talking about validation and my view model(via validation attributes) or my validation framework(via configuration like fluent validation) is responsible for my validation attributes on view, so engine should follow that logic and what I have specified and not to act on his own and put validation attributes where I didn't tell him to put it...do you agree?Jolley
@Marko, I don't agree. Think of it this way. In HTML you have input fields. Those input fields could be left blank/empty by the user. So you have to adapt your view model in such a way so that it can handle this case => by using nullable types. And of course since leaving this field empty contradicts your validation logic and requirements you simply decorate your view model property with the Required attribute (or instruct FlientValidation to require it). Your view model must be capable of hosting any user input (and in this case the user input could be blank).Endogenous
let us continue this discussion in chatJolley
I tried proposed solution from this link: #4767957 Fluent Validations. Error: Validation type names in unobtrusive client validation rules must be unique but asp.net mvc emits required attributes on each field regardless of AddImplicitRequiredAttribute settings...Jolley

© 2022 - 2024 — McMap. All rights reserved.