ASP.NET - MVC3 property decoration? - asp.net-mvc-3

Is there an attribute I can decorate a single property on my model to tell the engine not to include the property in the validation routine?
[DoNotValidate] or [ValidateIgnore]
----------------------More info.
Ok, I need to give you more information. In my situation, I have a temporary decimal value on my model that is not persisted, that gets formatted into currency. $540,000.
In this one case I do not want to strip the formatting out before I call TryUpdateModel. When you use TryupdateModel, it mvc will try and convert that string text box value back into a decimal and Model.IsValid will return false. I know how to get around this situation, using javascript, but it would be easier if I could tell mvc not to validate that field.

Any model properties not decorated with validation attributes should be ignored.
public class MyModel
{
[Required]
public string SomeProperty { get; set; }
public string IgnoredProperty { get; set; }
}
Should validate that SomeProperty is required, but nothing will happen with IgnoredProperty.
The best tutorial IMHO on Model validation is http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
(even though it says for MVC 2, it's applicable).

Change the type of your decimal to nullable decimal to prevent required validation:
public class MyModel
{
public decimal MyValidatingDecimal { get; set; }
public decimal? MyNonValidatingDecimal { get; set; }
}
MyValidatingDecimal will be required (since it is a value-type), while MyNonValidatingDecimal will not be required.

Properties will only be validated if you explicitly apply validation attributes to them.

Related

Is there a way in MVC3 to define data validation annotation for a group in a class?

I am new to MVC, recently I am working on the data validation, and I was wondering that in stead of giving a validation annotation for each parameter, is there a way that I can define validation rules for a group of parameters in a class? For example, a class is like this:
namespace MvcApplication1.Models
{
public class Product
{
public int Id { get; set; }
[Required]
[StringLength(10)]
public string Param1 { get; set; }
[Required]
public string Param2 { get; set; }
[DisplayName("Param3")]
[Required]
public string Param3 { get; set; }
}
}
Is there a way to define a rule that for Param1, Param2, and Param3, for example, at least 2 of them are required?
So easy to use. try this one.
MVC Foolproof Validation
and this is how you can build your own custom validation.
http://www.nickriggs.com/posts/build-model-aware-custom-validation-attributes-in-asp-net-mvc-2/
For more advanced validation like this, I would recommend FluentValidation
http://fluentvalidation.codeplex.com/
You can create your own custom validators:
http://fluentvalidation.codeplex.com/wikipage?title=Custom&referringTitle=Documentation
DataType
Specify the datatype of a property
DisplayName
specify the display name for a property.
DisplayFormat
specify the display format for a property like different format for Date proerty.
Required
Specify a property as required.
ReqularExpression
validate the value of a property by specified regular expression pattern.
Range
validate the value of a property with in a specified range of values.
StringLength
specify min and max length for a string property.
MaxLength
specify max length for a string property.
Bind
specify fields to include or exclude when adding parameter or form values to model properties.
ScaffoldColumn
specify fields for hiding from editor forms.

How can I have a required and nonSerialized attribute in my model

In my Model I have things like that :
[...]
public string PasswordConfirm { get; set; }
public string Captcha { get; set; }
[...]
I would like that these two attributes are required but not serializable
I tried tu use [required] and [nonSerialized] annontations but without success. I already saw this post
But I don't know how to do what I want. It will be helpful for NonObtrusive-Validation, i want these field complete but i don't want to serialized them.
I found a solution to my answer.
I can use the annotation [notMapped] which is compatible with [required], [compare] ...
I also use this :context.Configuration.ValidateOnSaveEnabled = false; where I filled my base because i got a problem with validation here.

ASP.NET MVC3 conditional validation - checkbox > editor

I would like to have a bool field and and a string field in my Model and not having any validation attributes on them.
But in the view I would like to have the required field validation on the Editor if the Checkbox is checked.
How do I do this please?
Thank you.
You can still use the data annotations attributes and follow any of this option.
Clear the errors from the modelstate dictionary for that field inside the action
Use the conditional validation library created by simon.
Ex.
public class ValidationSample
{
[RequiredIf("PropertyValidationDependsOn", true)]
public string PropertyToValidate { get; set; }
public bool PropertyValidationDependsOn { get; set; }
}

In ASP.NET MVC3 how do you stay DRY with very similar but slightly different viewmodels?

In building an app, we created a generic object model to store some values, the viewmodel looks a bit like this at the moment:
public class FooViewModel {
public int ID { get; set; }
public byte FooType { get; set; }
[Required]
[Display(Name = "Bar Name")]
public string Name { get; set; }
[Required]
public string Email { get; set; }
//etc, etc
}
The problem is: depending on the FooType, we want to have the Display Name to be different and the Email is not required for type 1 and 2, but is required for type 3 and 4.
We tried seperating out the properties that differ per type in to classes that inherit from this one, but the validation does a fallback on what is specified in the base type, so that didn't work.
Currently, the only option seems to be to create a viewmodel for each FooType (and also seperate controllers and view), which leads to a lot of code duplication.
What are other ways to keep this DRY?
To benefit a validation context (e.g. validating objects in different contexts), I strongly recommend using FluentValidation library.
You could implement a custom RequiredIf validation attribute, or you could implement IValidatableObject.

MVC DataAnnotations - Require at least one field in a group to be filled

How can I use DataAnnotations to validate that at least one of these fields are filled in?
public string Name { get; set; }
public string State { get; set;}
public string Zip { get; set;}
To do it using DataAnnotations you will need to make a custom attribute because as far as I know there is no built in attribute that will handle this.
To get you started, when you start a new MVC project there is a class called "PropertiesMustMatchAttribute" that is applied at the class level. You could base it off that without much difficulty

Resources