Validating two properties

Sep 28, 2010 at 6:48 PM

Hi Sacha,

I have another problem now. I have a EditableValidatingModel that have two decimal properties A and B. i need the validation to be like only one of them can be zero and one of them must be zero while the other holds some value. how can i set this for validation. Also, I haven't much understanding about how SimpleRule works. Please advice.

Sep 29, 2010 at 9:41 AM

OK so you will not be able to do that they way Cinch stands right now, but it is easily solved using existing codebase, and here is how.


You will need to make a new partial patr to the DataWrapper<T> partial class in your own user code, which would be like this

public partial class DataWrapper<T>
	public object ParentViewModel {get; set; }

	public DataWrapper(object parentViewModel, IParentablePropertyExposer parent,
            PropertyChangedEventArgs parentPropertyChangeArgs)
            : base(parent, parentPropertyChangeArgs)
		this.ParentViewModel  = parentViewModel;

	public DataWrapper(object parentViewModel, IParentablePropertyExposer parent,
            PropertyChangedEventArgs parentPropertyChangeArgs, Action valuesChangedCallBack)
            : base(parent, parentPropertyChangeArgs, valuesChangedCallBack)
		this.ParentViewModel  = parentViewModel;


Then anytime you create a new DataWrapper in your code, simply call this new constructor, so instead of doing this

ImageRating = new DataWrapper(this, imageRatingChangeArgs);

You would do this

ImageRating = new DataWrapper(this,this, imageRatingChangeArgs);

ok it looks strange but that because the old constructor casts the 2nd "this" to a special IParentablePropertyExposer interface, and doesn't expose it as a public property, so you need to pass in parent twice, and expose new 1st one, as public property of type object, which now means in your rules you can do this:

imageRatingRule = new SimpleRule("DataValue", "ImageRating must be between 1-5",
        (Object domainObject)=>
            DataWrapper<Int32> obj = (DataWrapper<Int32>)domainObject;

	    YourViewModelType parentViewModel = (YourViewModelType)obj.ParentViewModel;

	    //so now that you have instance of parent ViewModel you can do what you like such as 
	    if (parentViewModel.PropertyA.DataValue ==0 && parentViewModel.PropertyB.DataValue ==1)
		return false;

where you can get to the actual parent ViewModel (I have some dummy "YourViewModelType" above) so can get to any of its properties in your DataWrapper validation rules

Oct 1, 2010 at 5:34 PM

thanks sacha, i am doing it now.

Oct 2, 2010 at 8:17 AM

yeah that code should work fine, and it is something I will try and add to Cinch this weekend, along with a couple of other things people have asked for, which are all sensible suggestions that I do not mind adding in. Generally though with Cinch the core classes are all partial classes to allow such extensibility. And also the way the services work with MEF now, is great, you don't like the default Cinch services, write your own.

Oct 2, 2010 at 4:59 PM

I have added ParentViewModel property to DataWrapper now in latest release