ASP.NET MVC2 Client Validation bug? - validation

I enable client validation using the Html.EnableClientValidation method in my view. This client-side validation works great for text boxes, but I think I might have found a bug when used with dropdownboxes.
If you use the following construction Html.DropDownList( name, data, "Please choose..." ) without any ViewData-item with that name then client-side validation works great. If you look at the generated HTML code you will see that ASP.NET generated ValidationRules for it in the JSON block.
However, if I add a ViewData-item with that name then the ValidationRules for the client validation is empty!
In both cases, server-side validation works as expected. Bug or is there something that I am missing?

The solution is simple:
<%= Html.DropDownList("Username", CType(ViewData("Data"), SelectList), "Please choose...")%>
Client-validation does not work if you do it like this:
<%= Html.DropDownList("Username", "Please choose...")%>
In both cases, I use the same code to construct the ViewData item but it only works with the first statement.

Related

Symfony2 : Best way to check form, client side

I'm developing a form with Symfony2 : several text inputs and one file input (for one picture). I have defined some asserts (maxLength, minLength...) in my entity in order to check the form (isValid).
My problem is : if the user puts bad data in text input (text too long or too short...), he still can submit the form, and error and printed but the user have to re-choose his picture.
As I think it's impossible to keep the picture in the form after bad validation, I should maybe check the form in client side (javascript), before submit.
So, is there an automatic way to do this (to forbidden submit until data are correct)? Can we get the assert minLength, maxLength value in twig ?
Thank you !
Ben.
You can use js validation before submitting the data, using some js form validation tools, but this way you need the replicate the validation logic from the server, so if validation rules changes, you need to modify on both server and client side. I recommend this method to reduce the traffic between client-server.
If you don't want this, use ajax form submitting (example here). You still validate the form using symfony, but the page won't refresh, so you won't lose the attached file. But this generates additional traffic to server, and you also need to implement error displaying using javascript.

Displaying model state errors after ajax call on Razor views

I have razor view with #Html.ValidationMessageFor helpers and jquery unobtrusive validation setup.
I want to call controller/action and to show eventual model state errors returned by action by using same validation logic that is already set in place.
I've made some code that does it but I was wondering if there is already way to do it automatically, i.e. if I capture HTTP Bad Request as AJAX response, I want to take out model state errors from response body and plug them in to unobtrusive validation.
I'm looking for complete recommended solution, not workarounds :)
Thanks!
You can return errors with Json result (How to get all Errors from asp.net mvc modelState?):
var allErrors = ModelState.Values.SelectMany(v => v.Errors);
Then manually show errors. Get form validator:
var validator = $("form").validate();
Then check that your fields are initialized correctly, for example you can look here (optional step):
validator.settings.rules
OR
validator.settings.messages
If everything is fine, then you could show error:
validator.showErrors({"Password": "Too simple!"});
Where Password is field name and Too simple! is error message.

Validation - Do I need to show what is not validated in PHP if I use JS?

Okey, this might seem a bit strange question so I will explain.
Do I really need to create a postback that explains what is wrong with form if it's not validated if I also use JS for it?
I am of course validating user input and I use somewhat "general" approach. For instance if something is not validated it will just show "Some error occurred, check your input bla bla..". I am not creating postback for every input so that it will shot "Your username is suppose to be at least 3 characters long etc.." and I don't do this because JS is doing that on the fly.
My server-side validation only is like a guard against stupid/wrong entries where name is empty or something along that, rest is up to jQuery. Form will always be valid if client is running JS. I am doing it to save my time.
My question is - is it a bad idea? I just don't see why because everyone is running JS anyway and my server is not allowing bad/invalid entries to be put in DB even with JS off.
I don't think that's a bad idea, data validation can be client side. If something goes wrong, i just throw a generic error.
I only validate server side the business rules

Remote Validation with MVC3

I've just been reading the article on MSDN about remote validation. This is great, but it only shows validating a specific property value.
Is there a way I can pass other values from my model into the validation for a particular property? For example, let's say that a user wants to cancel a number of items off an order - they should be prevented from entering a figure greater than the original order amount.
Thanks
No, you can't.
Brad Wilson:
At this time, only property level
validators can emit client-side
validation (as that lines up much
better with the idea of input
validation in the form of the
browser... there is no "model" to
speak of, from the browser's point of
view).
Stuart Leeks:
I don't believe you can hook up client
validation with IValidatableObject
Well, i am nit sure if you mean this, but you can use AdditionalFields with your RemoteValidation attribute.
Remote Validation in ASP.Net MVC 3: How to use AdditionalFields in Action Method

asp.net mvc client side validation for camel case property name

I am using the MS shipped client side validation in asp.net mvc 2. The model in question has one property called "FirstName". Our client side developer really like to have camel-case in the elements id, so instead of using the normal html helper Html.TextBoxFor(m => m.FirstName), we wrote out the html input view instead like: <input type="text" id="firstName" name="firstName" />. The model binder can bind correctly and get the right valud ( I guess it was not case sensitive, which is a good thing). However, when we turn on client side valuation and issue a Html.ValidateFor(m => m.FirstName) at the end, it still generates the Pascal-case format of the property (which is expected).
I look into the mvc 2 source code reveils that ValidateFor() calls ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData) which in turn uses MemberExpression to get the property name (which is pascal case). I am wondering if there is way around this? The ultimate goal is to have camel-case ID is the elements of the html and still have both client and server side validation works.
Any help is appreciated.
My $0.02: Pick a casing and make the view model match the page. Both C# and JS are case-sensitive, and attempting to mix cases won't end well. One of you is going to have to change case. You could probably work around this specific issue, but it won't be the end of your problems.

Resources