MVC3 selectively validate client side - asp.net-mvc-3

Each form in the application has a set of radiobuttons. Once selected, only certain fields associated with that radiobutton will need to be validated.
I am using MVC 3 and need the validation to work client side.
Simply using DataAnnotations I can only validate all fields on the form.
IValidatableObject doesn't work clientside.
IClientValidatable looks like it might do the job, but it seems I would have to write a new attribute for every standard DataAnnotation attribute.
RemoteValidation works with one field at a time.
Another option would be to drop MVC3 validation and do it all using jQuery. I don't have a problem with this as such but would like to use MVC3 and reduce coding/maintenance in preparation for a much larger project.
Could I still use MVC3 validation but then use jQuery to add/remove validation fields from validation whenever a radiobutton is selected?
If anyone can help with some suggestions as to the best way to approach this, it would be much appreciated.

MVC 3 uses jQuery's validation plug-in by default and that plug-in will not validate disabled fields. Are the fields that you don't want to validate no longer needed if certain radio buttons are selected? If so, then you can just disable those elements and they won't be validated (and note that those disabled fields won't be posted to the server either).
e.g.
$('input').attr('disabled', 'disabled');

For complex validation it is best to hand code these.
Data Annotations work great for 90% of your validation needs, but fail dismally with What/If scenarios.
For the client side use an event driven custom validation presented via jQuery Validation Plugin. For the server, use the CustomValidation attribute:
http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute(v=vs.95).aspx
Using IClientValidatable is great if you have reusable custom validation, however it is wasted time for one off validations.
Alternatively use RemotValidation with a CustomValidation attribute that invalidates multiple fields.

Related

How to make model field conditionally required in MVC3?

I have a view with a dropdown and a Rich Textbox. This view is associated with a model. I would like to make Rich Textbox field is required based on the value selected in dropdownlist.
is there any out of the box feature available in MVC3 to do this?
I have had similar requirements in the past, I solved them using FoolProof. It provides extra validation objects such as requiredIf etc.
Only issue I have had is with the JS file, it can be a bit buggy around dates and date handling, other than that, it is ace.
Foolproof site

yii ajax renderpartial with form validation

I have a dropdown list in yii that depending on the id renders some additional fields (that requests other models).
How can I use the $form field in those (it's Yii-Bootstrap) so that I can have the validation messages (via $form->errorSummary() ) and not implementing my own javascript messages?
I'm not entirely sure what your asking for, but it seems like it's related to rendering form fields/errors and validation. As far as specifing custom selector fields goes, look into CHthml::activeDropDownList, it'll let you define your drop list items accordingly. As for validation, you can validate that drop down by making it have it's own validation rule and error as described here:Custom Model Validators. This will allow you to use CHtml::errorSummary($model) as the dropdown will return an appropriate error message if it's validation fails...
Cheers,
Fy
Not sure if this helps, but renderPartial() has serious issues with AJAX:
http://www.yiiframework.com/forum/index.php?/topic/10427-ajax-clientscript

Integrating Ext.grid.panel validation and Ext.data.Model.validations

I've been learning ExtJS4 after having done quite a bit of dev in ExtJS3. I'm quite intrigued by the new class Ext.data.Models, but I would love to integrate these validations with the validation function in Ext.grid.Panel.
Can anyone point me in the direction of any examples of using the validations property of Ext.data.Model in a Grid panel?
I've tried adding the validations to the model and putting invalid values in the grid, but it doesn't seem to throw an errors or the normal red lines.
Any ideas?
Model validation against grid data is not supported out of the box currently.
Here is a working extension for model validation against form fields though.
And here is an incomplete attempt for model validation against a grid (what you were going for).
#Drew
The grid provides RowEditing and CellEditing plugins for row/cell editing. In the background these plugins use Form panel for the validation of the input. So, you can use the form panel extension that #Geronimo has mentioned along with the extensions of RowEditing and CellEditing classes and use them in your grid to validate the data entered in the grid against the model associated with the row. And since, the validate() method is on a model, which can be used to validate a complete row data or a particular cell data. In case you are looking for bulk validation, you can override the sync() method of the Ext.data.Store class to achieve that.

User-friendly message for when user enters html

Have an ASP.NET MVC3 app with model validation using FluentValidation. User enters some html in a regular text field, and is presented with a user-friendly error on submit (because of the "A potentially dangerous Request.Form value" error).
What I'd really like to do is show a validation message right on the form itself ("Oops, no html allowed") which shows up right next to the field - similar to model validation errors. That way, the user (malicious or not), knows about it before the entire form is actually filled out and submitted.
Would appreciate any ideas on how to go about implementing this apart from what's shown here (i.e. without having to add [AllowHtml] + a regex to almost every field in almost every viewmodel)
Thanks!
You will need to write a custom ProertyValidator for server-side FluentValidation with client-side support. I think you can follow this article, here, to create client-side jquery validation rule and the adapter.

Struts2 validation and Select tag

I'm using struts2 and validation using annotations in the action. I do use another action to fill the select lists when i fill the form with missing data it returns to the input result of the action but with the missing list and shows me an error.
See the FAQ entry about repopulating form controls when validation fails.
The nutshell version is that if you have lists they need to be repopulated using either the Preparable interface or with <s:action> tags. Personally, I think Preparable is the way to go.

Resources