New Posts New Posts RSS Feed: Client side validation
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Client side validation

 Post Reply Post Reply
Author
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Topic: Client side validation
    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
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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. 
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post 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
 
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down