Topics: Prism v2 - Silverlight 2, Prism v2 - WPF 3.5
Jan 26, 2009 at 7:12 AM

Is there any roadmap for Validation? I have checked the validation that you guys implements in RI sample. I found that we can't click the button if the validation is not success. But if we are binding the Entity class with View, it's so hard to disable the button.

Let's say we have a entity class called Employee. We bind this class with View.

For example:

<Textbox x:Name="employeeIDTextbox" Text = "{Binding ActiveEmployee.EmployeeID}" />
<Textbox x:Name="firstNameTextbox" Text = "{Binding ActiveEmployee.FirstName}" />
<Textbox x:Name="lastNameIDTextbox" Text = "{Binding ActiveEmployee.LastName}" />

<Button Content="Save" />

Those three fields are mandatory fields so that the user has to enter it. and FirstName and LastName should be less than 50 characters.

If I have those three properties in Presenter or ViewModel instead of Entity class, I can add this validation in Setter and handle disabling\enabling the "Save" button. But those properties are in Entity class so that I'm not sure how to validate it. I know that it's possible to use the event to notify the changes from Entity class to ViewModel but it might be too expensive..

How I can validate this scenerio?

If I don't want to disable/enable the button, how I can validate in SaveCommand. For example: bool isValidationPassed = ValidateRule.Validate(ActivateEmployee);


<Textbox x:Name="employeeIDTextbox" Text = "{Binding ActiveEmployee.EmployeeID}" />

Feb 13, 2009 at 4:32 AM
I am not sure I fully understand what you are asking. But here is what I think it is.

How can you disable a save/enable a save button if the validation rules are broken in your 'entity' object?

//First name is required.
<Textbox x:Name="firstNameTextbox" Text = "{Binding ActiveEmployee.FirstName}" />

Assuming ActiveEmployee is a property on your View Model, and that FirstName is a property on your 'Entity' object. Databinding will set the value of your textbox to the FirstName property of your 'Entity' object. At that point you will need you have your 'Entity' run its validation. If your entity fails validation you will have to set some some value on your entity such as IsValid = false.

public string FirstName
    get{return mFirstName;}
    set{mFirstName = value; CheckRules();}

public void CheckRules()
        IsValue = false;

Your SaveCommand in your ViewModel will then check IsValid value in its CanSaveExecute().

public bool CanSaveExecute(object parameter)
        return true;
    return false;

Hope this helps,