Print Page | Close Window

Client side validation

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3332
Printed Date: 12-May-2026 at 7:33pm


Topic: Client side validation
Posted By: pponzano
Subject: Client side validation
Date Posted: 12-Mar-2012 at 3:21am
Hello,
I've got an object I share between client/server defined as

[DataContract]
public class TestClass
{
[DataMember]
public int ID {get;set;}
[DataMember]
public string  Description {get;set;}
[DataMember]
public decimal Value{get,set;}
}

I've it defined in a viewmodel I use to insert this kind of data

public class MyViewModel : Screen
{
public TestClass myTestClass
{
get
{
return _myTestClass
}
set
{
_myTestClass = value;
...
}
}
}

how can I validate this object ? I've done other validation using the method proposed at http://www.lyquidity.com/devblog/?p=71 but I've not the single properties in my viewmodel, just the poco object

Thanks



Replies:
Posted By: mgood
Date Posted: 12-Mar-2012 at 7:32pm
I forget if you are doing WPF or Silverlight, but you'll have to implement IDataErrorInfo (WPF) or INotifyDataErrorInfo (Silverlight) in the POCO. You also have the option of simply throwing exceptions in your setters and set ValidatesOnExceptions to true in the binding. Neither DevForce nor Cocktail currently provides anything that helps you with implementing validation in your own POCOs. This is something we are considering, but at the moment you'll have to implement it yourself. 


Posted By: pponzano
Date Posted: 13-Mar-2012 at 1:08am
Hello Marcel,
in my testing I did so, but I've got a problem... I need to disable the ok button based on if there're validation error.... using IDataError info, how do I check if there're error?


Posted By: mgood
Date Posted: 14-Mar-2012 at 9:56am
You are better off adding a boolean property to your object that indicates whether it has validation errors or not. IDataErrorInfo doesn't lend itself for this task.


Posted By: pponzano
Date Posted: 15-Mar-2012 at 12:15am
Hello,
I've adopted an hybrid approach
here's what I did
 

[DataContract]

public class OperazioneInsert : INotifyPropertyChanged, IDataErrorInfo

{

    public OperazioneInsert()

    {

        ListaErrori = new ObservableCollection<string>();

        ListaErrori.CollectionChanged += (sender, e) => OnPropertyChanged("ListaErrori");

    }

 

    public ObservableCollection<string> ListaErrori { get; set; }

 

    public bool HasError

    {

        get { return ListaErrori.Any(); }

    }

[...]

public string Error

    {

        get { return string.Empty; }

    }

 

    public string this[string columnName]

    {

        get

        {

          string error = string.Empty;
          [logic for checking error on item

default:

                  return string.Empty;

          }

 

          if (!string.IsNullOrEmpty(error))

          {

              if (!ListaErrori.Contains(columnName))

                  ListaErrori.Add(columnName);

          }

          else

          {

              if (ListaErrori.Contains(columnName))

                  ListaErrori.Remove(columnName);

          }

 

          return error;

]
 
}
 
In the viewmodel I check if there's an error... btw my preferred approach is DataAnnotation but I'm not able to get it to work
 
 



Print Page | Close Window