Add validation group is needed

Dec 4, 2008 at 11:03 AM
  Validation Aspects is a good framework, but when I try to use it I find it lost a very important function: validation group, this make it difficult if you want to use only parts of validation attribute in a defined object once.
  What I suggest is add a ValidationGroup parameter in attribute, when your want to valid, just do Validate(ValidationGroup), ValidationGroup parameter can be empty, that mean it's used in every group, the original Validate() is also work, it means to check all validators, ValidationGroup is a string splited by comma like [NotNullOrEmpty("group1,group2")].
  Really hope it to be added in coming version!
Coordinator
Dec 4, 2008 at 11:36 AM
Hi yuxinbo,
Can you provide me with a real world use case? I need a little more detail to understand what issues this proposed partial validation feature solves.

Thanks
Mike
Dec 4, 2008 at 11:56 AM
Hi mikesaunders,
What I encountered problem is now I work with a workflow project. When user input one form he can save the temporary data into DB, but that doesn't mean this form will be submit, only because he want to input next time continuously, so only format check is necessary, required validation can be ignored. When user want to submit his completed form, format and required validation is both needed this time.
So I think ValidationGroup is useful in this case like asp.net' validation compontent also provided ValidationGroup.

Regards

Dio yu
Shelley Winters - "Whenever you want to marry someone, go have lunch with his ex-wife."

On Thu, Dec 4, 2008 at 8:36 PM, mikesaunders <notifications@codeplex.com> wrote:

From: mikesaunders

Hi yuxinbo,
Can you provide me with a real world use case? I need a little more detail to understand what issues this proposed partial validation feature solves.

Thanks
Mike

Read the full discussion online.

To add a post to this discussion, reply to this email (ValidationAspects@discussions.codeplex.com)

To start a new discussion for this project, email ValidationAspects@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Coordinator
Dec 4, 2008 at 7:20 PM
Hi,
this looks quite interesting and I can see why it is useful. To implement the feature as per your suggestion is not hard to do. I want to look into how and why other frameworks support this first before I commit to a change, just so I fully understand the wider picture.

I wonder if the following might meet your needs - you can do this currently with Validation Aspects:

I assume you can query the model/entity's state to determin how it should be validated, ie which validation group(s) to validate on. If so, you can write validation functions that query the model and decide if they should validate or not. Consider the following validation function on a model:
typeof(IModel).AddValidation<IModel>((model, instance) =>
{
  // only perform validation if model is in state XYZ
  // query the model's state by some means
  if (model.State != XYZ) return;
  // perform validation
  if (string.IsNullOrEmpty(model.CustomerName))
    throw new ValidationException(...);
  if (model.Total < 0.0)
    throw new ValidationException(...);
 });

IModel instance = new Model();
ValidationResult result = instance.Validate();

I appreciate it's a little more involved than just using Attributes but it does mean that you can encapsulate what to validate and when inside a validation function rather than externally making assumptions of how to validate a model, eg model.Validate("PhaseN").
Dec 8, 2008 at 6:29 AM
Hi,
    I appreciate the solution you provided, it is useful in many scenarios. although need to add a state mark in entity and some piece of codes for each state. But it can solve the problem now. Hope validation group function will be added in the future.

    I'm also worried about customizing error message for every property, although it can solve by the way you proposed above or using validation method in object, is there a more easy way for it?
Coordinator
Dec 8, 2008 at 9:13 AM
You can use the exception's context property to put the property name in the exception message. Something like:

ValidationSettings.FormatExceptionMessage.NotNullOrEmpty(e => string.Format("{0} is required.", e.Context.Property.Name));

You set this once.

[NotNullOrEmpty] public string Username { get; set; }
[NotNullOrEmpty] public string Email { get; set; }

The failing validation exception messages for the above properties will be:
Username is required.
Email is required.

You'd probably want to check the Context for the existence of a property as the exception could be thrown from the failed validation of a method parameter instead.

Let me know if this answers your question.


Dec 26, 2008 at 11:36 AM
Hi mikesaunders,

Sorry for later reply, I must say the approach is work well with English, but if for other language, the property name usually is different with meaning. In this situation, Can you kindly tell me the solution?

Regards

Dio yu
Coordinator
Dec 29, 2008 at 1:23 PM
For localization I would use the PropertyName as the key in a lookup table to get the localized name. I typically use resx files and satellite assemblies for the localization of desktop applications. I would also localize the exception message the same way.

ValidationSettings.FormatExceptionMessage.NotNullOrEmpty(e => string.Format(ValidationMessages.NotNullOrEmpty, PropertyNames.ResourceManager.GetString(e.Context.Property.Name)));

In the above, both ValidationMessages and PropertyNames are the static classes generated from building a .net Resource File. The value for ValidationMessages.NotNullOrEmpty would be something like {0} is required.
Jan 5, 2009 at 2:59 AM
Hi mikesaunders,
Thanks for your helps, now I hope for new version quickly release.O(∩_∩)O~

Sent: Monday, December 29, 2008 10:23 PM
Subject: Re: Add validation group is needed [ValidationAspects:41473]

From: mikesaunders

For localization I would use the PropertyName as the key in a lookup table to get the localized name. I typically use resx files and satellite assemblies for the localization of desktop applications. I would also localize the exception message the same way.

ValidationSettings.FormatExceptionMessage.NotNullOrEmpty(e => string.Format(ValidationMessages.NotNullOrEmpty, PropertyNames.ResourceManager.GetString(e.Context.Property.Name)));

In the above, both ValidationMessages and PropertyNames are the static classes generated from building a .net Resource File. The value for ValidationMessages.NotNullOrEmpty would be something like {0} is required.