Force validation (for Gui)

Jan 28, 2012 at 2:04 PM

Hi Sacha,


I have this class with a validationrule, that uses more than one props.
To prevent firering this rule on the fill-cycle, I have expand your EditableValidatingObject with bool IsFilling.
This Prop can be used in the Rule.

public class CustomerUI : EditableValidatingObject
{
    Name1 = new DataWrapper<string>(this, null);
    Name.AddRule(_CusomerExistsRule);
    Name2 = new DataWrapper<string>(this, null);
    BirthDate = new DataWrapper<DateTime?>(this, null);

    public void Fill()
    {
        IsFilling = true;    // new
        Name1.DataValue = "Bill"
        Name2.DataValue = "Clinton"
        BirthDate.DataValue = ...
        IsFilling = false;
    }   
}    


After the filling, I want to force the WPF-Validation.
To Force rule for Name1 I can write:
Name1.DataValue = Name1.DataValue;

But what, if I have 78 Props wiht 23 rules?

My question: what's the generic way? How can I "rewrite" all DataValues, that have on or more validationrules?
Possible without reflection?

I tryed this over the _CachedListOfDataWrappers, but there i don't have access to the DataValue.

Best regards
Christoph

 

Jan 29, 2012 at 9:30 AM

I think what you could do is after you set IsFilling you could simply call the IDataErrorInfo interfaces Item indexer passing in empty string, that is what WPF should do. Cinch has a property for this already called Error, So you could try something like this after setting IsFilling 

 

string dummy = this.Error

 

You could try that, failing that, you would have to think up something cool.

Jan 29, 2012 at 6:09 PM
Edited Jan 29, 2012 at 7:26 PM

Thanks for your reply at sunday :-)

Edit#1: I found this, it solves the problem!

string dummy = this.Error works partially, see the first comment.

The second comment: I have added bool HasRules to ValidatingObject
It works, but its not so elegant...

foreach (var dataWrapper in _CachedListOfDataWrappers)
{
// this works partially; the Rules are evaluated, but this raises not to the wpf.
        var dummy = dataWrapper.Error;        
        if (dummy != null)
            Debug.WriteLine(dummy);

// this works, but isn't generic
        var validatingObject = dataWrapper as ValidatingObject;
        if (validatingObject != null && validatingObject.HasRules())
        {
             if (dataWrapper is DataWrapper<string>)
                 ((DataWrapper<string>)dataWrapper).DataValue = ((DataWrapper<string>)dataWrapper).DataValue;
             // todo for all (!) other types
        }
}

Best regards,

christoph

Jan 30, 2012 at 7:54 AM

Cool glad you are sorted. Problem with this one is its more about WPF, not Cinch.

In SL there is a new interface which notifies, can't quite recall its name, but we have done some work bringing that interface into WPF at work, and have working codebasde which allows our errors to notify which means we no longer have to resolve to hacks (like the one you point to in that post).

I will try and dig it out, and put it on drop box, or something.

It will not be included in Cinch though (sadly), as it would just require too much work, and I am too busy on other things.

Jan 30, 2012 at 10:07 AM

So at work we don't use Cinch exactly but something like it. For validation we have these things called Validators which do validation for us. We also use that push notifying validation that I mentioned. We call Validator.Validate() when a property changes by implementing our own version of that SL interface which is called "INotifyDataErrorInfo".

 

I have put a small demo project here : http://dl.dropbox.com/u/2600965/SmallValidationDemo.zip

 

Enjoy

 

But like I say this will NOT be part of Cinch any time soon (as it would require too much work to get it in there)

Jan 30, 2012 at 7:33 PM
Edited Jan 30, 2012 at 7:34 PM

Many thanks for your Validation-FW. The great benefit: the dynamic Error-description :-)
I have just see the SL-INotifyDataErrorInfo, but my skills are too low for an own implementation...

-christoph

Jan 31, 2012 at 7:40 AM
Edited Jan 31, 2012 at 7:43 AM

No worries, you don't really need many skills its all there, all you need to do is

  • Get all broken rules by doing this on your Validating object/viewmodel

GetBrokenRules(""), you will need to do that for all datawrappers too,  this will result in a ReadOnlyCollection<Rule>, which you will  need to keep adding to as you get the complete list of broken rules 

  • Pass the list of broken rules to a Validator like the one in that validation framework
  • Then just wire up the View to the Validator as I do in that validation framework 

Should be easy enough


It's all in Cinch or that validation framework

Feb 15, 2012 at 9:08 AM
Edited Feb 15, 2012 at 9:08 AM

Hi Sacha,

what do you mean, is it possible for me (I have moderade knowledge of Cinchs validation), to make "SmallValidation" as a part of cinch?

-christoph

Feb 15, 2012 at 10:33 AM

No I was just saying if that small demo project I posted link to was of use to you, it should not take you that much effort to hook it into existing Cinch code if you wanted to do that. If you don't want to, no bother