ASP.NET MVC 3 validation order - validation

As part of our ASP.NET MVC3 project, we have implemented some custom validation. On a particular entity e.g. UniqueMandatoryCode, we have got [Required] and our [CustomValidationDataAnnotation].
They both work but I would like to know what is happening under the hood in terms of the order of execution for validation. The issue I have is that our CustomValidation code is hit before the [Required] validation. This poses problems when we pass empty values.
So the question is, how do I control the order of validation i.e. first go through the [Required] validation and then the [CustomValidationDataAnnotation] validation.

I guess the validation order cannot be easily controlled.
The common technique is to ignore the empty/unspecified case in all other validators (ignore = you treat it as valid). You will anyway add a required validator if the value is mandatory that will handle that case. If the value is optional, why would you apply the custom validation rule on an empty/unspecified value?

Related

Does fluent API IsRequired() does exactly the same as [Required] attribute over the property?

If I put [Required] attribue over a property in a model then the <input> helper tag will throw out a validation error if no value is provided, but also it will be affecting database structure.
However does this
modelBuilder.Entity<CartItem>(e =>
{
e.Property(e=>e.Quantity).IsRequired();
}
do the same? Meaning is this causing somehow dynamically adding [Required] attribute to a property so <input> tag helper can notice it?
"Meaning is this causing somehow dynamically adding [Required]
attribute to a property so tag helper can notice it?"
Yes, exactly it is. IsRequired and the [Required] will eventually have the same impact on database schema as it will add a non-nullable constrain on the table column. Means the property would be mandatory. The key difference is we only can use fluent API validation code first's approach while [Required] all approach.
In fact, while using e.Property(e=>e.Quantity).IsRequired() attribute fluent API instead of annotations to get the same client side & server side validation. Rather than use Required. The thing is validation errors thrown based on the Fluent API configurations will not automatically reach the UI, but you can capture it in code and then respond to it accordingly.In addition, you can check the official document here

Disable Grails Scaffolding Validation for one Class

We use the Grails scaffolding for our internal part of our web app. Now we have the requirement to modify some values in the before validate method. The Problem is, that these fields are blank: false or nullable: true
It's no problem to modify these values in the beforeValidate method, but the problem is, that the scaffolding adds a required attribute to the html form, and so we can't submit it with these empty fields.
Is there a way to disable the scaffolding validation on a specific view for a specific class?
Greetings
The short answer is "No, there isn't a way to turn of validation for a specific domain class in scaffolding."
However, you can always generate the views for the domain class and edit the GSPs to remove the required attributes on the fields in question.
grails generate-views com.somewhere.MyDomainClass
Scaffolding is to get you a starting point to work from, not continued use and customization.

Spring Framework Validation - optional validation of entire form?

I have a Spring-based web form (Web Flow actually) that I need to modify.
The form has multiple elements that are validated server-side. The tricky part is, the whole form is optional - if the user just clicks past it, I'm supposed to quietly ignore the whole thing.
How can I do this?
I assume you use JSR303 Bean Validation. But the solution depends strongly on what you exactly mean by "the whole form is optional",
do you mean each value is independent optional: ----
The most validation rules are designed to accept Null Values (except you explicite permit Nulls (#NotNull, #NotEmpty) - maybe this would be enough for your probelm.
do you mean that the whole form needs to be valid, if at least one value is set: ----
JSR303 Bean Validation knows the concept of validation groups: A rule gets only validated if its validation group is requested to be validated (All not explicit assigned validation rules belong the the default group). You can use this to design a validation group for your optional values, and then start the valdation for this group explicite only when at least one of its values is set. This can be done by implementing a custom DefaultGroupSequenceProvider for the form. 5.4.2. #GroupSequenceProvider

ASP.NET MVC 3: model-level client-side or remote validation

I need to do some model-level validation in an MVC 3 edit page. (To be specific, I need to confirm that either Field A or Field B is filled in, but not both and not neither.)
I want to perform client-side validation as well as server-side validation, which means either using remote validation or implementing duplicate validation code. I'm OK with either.
I've read a number of posts on rolling your own server-side model-level validation, but none that deal with also implementing client side validation. (I don't know -- I'm sure someone out there can tell me -- whether model-level client-side validation is easy to set up with jQuery validation.)
I've also read about implementing your own remote validation from scratch, which I may have to do since the Remote attribute is property-level only.
I've read this question, which is identical to mine, but the only link that's really on-point doesn't seem to say what the answerer says it says.
So, my question: is there an easy, relatively low-effort way to implement server+client model-level validation, with or without a remote component? And is there a nice blog post or web page somewhere that explains this?
I think Data Annotation Extention by Scott Kirkland does exactly what you want.
Here is a blog post he wrote about his extensions.
The core library provides server-side validation attributes that can be used in any .NET 4.0 project (no MVC dependency). There is also an easily pluggable client-side validation library which can be used in ASP.NET MVC 3 projects using unobtrusive jquery validation (only MVC3 included javascript files are required).
There is a similar question answered here which may be of some help? The answer given is for validating that at least one field is entered but the principles given in the answer may be what you are looking for and you should be able to change the answer to suit the validation you require. The solution also offers both server and client side validation options and I believe you can use the solution as both module or property level validation?
Additionally, there is also the following article here detailing how to create your own custom validation similar to that provided in the answer I linked to.
Hope this helps.
If i get you right, mvc 3 do actually include jquery client-side validation.
First, for the model-level server side validation, you can override the default isValid function with your own validation rules, something like this (involve multi-field):
public sealed class PropertyAAttribute : ValidationAttribute
{
public string propertyBAttribute { get; set; }
public override bool IsValid(object value)
{
// Your validation rule here
}
}
[PropertyA(propertyBAttribute = "PropertyB")]
public object PropertyA {get;set;}
public object PropertyB {get;set;}
Then, to deal with client side, you can simply use the included jquery validation function:
var frm = $('#formData');
frm.validate();
Like this, you will have the error message at client side based on the rule you defined in the model.
Hope this is what you need :)

Conditional validation groups in MVC3

I have an entity framework generated Model. When the model is initially created I only want to require a few properties, but afterwards I want to require a few more. Is there any notion of validation groups or conditional validation attributes that would help me here?
I tried writing a custom conditional validation attribute that would just take another validation attribute in its parameter and just encapsulate other validation attributes but I get compiler errors saying "An attribute argument must be a constant express, typeof expression or array creation expression of an attribute parameter type"
Any idea how to accomplish this?
I have an entity framework generated Model
That's nice but not something that controllers should pass and take from views. In order to decouple the domain entities which, in theory, should be reusable among different applications from the way those entities are presented on a given view you could use view models. Those are classes which are specifically designed to the requirements of a given view. Thus you could have a CreateFooViewModel and UpdateFooViewModel both representing some domain model but of course with different validation rules (as validation rules differ between your views) and different formatting rules and properties selection. In order to ease the mapping between your view models and domain models you could use AutoMapper.

Resources