Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Client-side Validation RSS

4 replies

Last post Jan 14, 2010 03:48 PM by ManuelFelicio

(0)
  • ManuelFelicio

    ManuelFelicio

    Participant

    986 Points

    238 Posts

    Client-side Validation

    Dec 21, 2009 11:29 AM | LINK

    Hi,

    I'm trying to understand how to properly do client-side validation in my entities.

    I've got some validation attributes (including CustomValidationAttributes using shared validation code) in my metadata. I also have my validation error message strings in linked resource files between server and client. Everything works well.

    I was now exploring the ValidationSummary control and I notice that it is showing all my validation errors as expected. What I do not understand is why the Entity.ValidationErrors property is not "synchronized" with the validation errors that are being thrown by the DataBinding framework. My entities' properties are never set with the "invalid" values because the setter validates the new value first. That means that my entity is always in a Unmodified state, although I wanted to modify it. I would like to bind the ValidationSummary.ItemsSource property to my Entity.ValidationErrors property.

    After digging deeper on this issue I noticed that the ValidationErrors property is only set on the server-side validation. Is this the expected behavior?

    I'm using MVVM and my view's have no codebehind and I would like my viewmodels to know about validation errors right before I try to save changes back to the server. I don't want a round-trip to the server just to have the ValidationErrors property being set with the errors that had already been thrown by the DataBinding framework.

    The following code is called by a save command on my viewmodel:

    1    protected virtual void Save()
    2            {
    3                if (this.EditMode != EditMode.ReadOnly)
    4                {
    5                    this.Entity.EndEdit();
    6                    if (!this.Entity.HasValidationErrors)
    7                        this.DomainContext.SubmitChanges(this.SubmitChangesCallback, null);
    8                }
    9            }

    The HasValidationErrors property is always false and even though the user typed invalid values on the UI I can still submit changes to the server (which doesn't do anything at all because the entity is still in Unmodified state).

    I guess I have to check the entity state instead of the HasValidationErrors property, but still I'd like to know the errors.

    Any help on this is very apreciated,

    Thanks.

     

  • Min-Hong Tang - MSFT

    Min-Hong Tan...

    All-Star

    28750 Points

    3295 Posts

    Re: Client-side Validation

    Dec 24, 2009 03:59 AM | LINK

    Hi,

        Em, this property is set to true only if you got errors when last submit.

        It is described in this document.

       http://msdn.microsoft.com/en-us/library/system.windows.ria.entity.hasvalidationerrors(VS.91).aspx

       Maybe you need to use Entity.Validateproperty method

    Best Regards

    Min-Hong Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • ManuelFelicio

    ManuelFelicio

    Participant

    986 Points

    238 Posts

    Re: Client-side Validation

    Dec 26, 2009 09:16 AM | LINK

     Hi,

    I know that. The problem is that I shouldn't need to make a submit just to update my Entity.ValidationErrors property because I already know that my entity has validation errors.

    ValidateProperty simply throws an exception. The ValidationErrors property is only set when the SubmitOperation completes.

    MF.

  • ManuelFelicio

    ManuelFelicio

    Participant

    986 Points

    238 Posts

    Re: Client-side Validation

    Jan 12, 2010 02:55 PM | LINK

    Still unanswered :(

    What I would like to achieve is have my viewmodels know the validation errors that are thrown through databind. The problem is that if my user's input is invalid it will never be set on my entity properties and my viewmodel has no clue if the UI is displaying a validation error or not. I bind directly to my entity and I'm not using DomainDataSource or any other RIA controls.

    Is there a best practice for this? How do you check for errors before submit? 

  • ManuelFelicio

    ManuelFelicio

    Participant

    986 Points

    238 Posts

    Re: Client-side Validation

    Jan 14, 2010 03:48 PM | LINK

    I found this blog post about knowing the binding validation errors in a viewmodel and it seems to be just what I was looking for.

    http://www.thejoyofcode.com/Silverlight_Validation_and_ViewModel.aspx

    It relies on the BindingValidationError event in the FrameworkElement class so we can use an attached property that holds all the errors fired by that event and bind them into a collection in our viewmodel.

    Problem solved.

    Thks