Skip to main content

Microsoft Silverlight

Answered Question User control input validation considerationRSS Feed

(0)

Jessmmy
Jessmmy

Member

Member

54 points

64 Posts

User control input validation consideration

Guys,

I have a question about how to implement user input validation here.

System.ComponentModel.DataAnnotations attributes can be added to business objects to carry out the validation in conjunction with control bindings. To do this, we get make use of all these data input validation controls.

 In my case, I just want to add some validation to some search criteria, like required field, data format and etc. Which approach should I go for? Should I just implement my owner logic by writing up some functions?

 Thanks 

 


I can do it!

Sledge70
Sledge70

Contributor

Contributor

5892 points

1,042 Posts

Re: User control input validation consideration

Hard question to answer really as it probably comes down to personal preference.

I'd build an entity that implements INotifyPropertyChanged with all my validation in it and bind that up to xaml and use the built in visualstates on the toolkit controls.

You can take control of when validation occurs by using the method I posted in this thread:
http://silverlight.net/forums/t/111830.aspx

______________________________________________________
Please mark replies as answers if they answered your question...

Flexman on Silverlight

jessmmy
jessmmy

Member

Member

54 points

64 Posts

Re: User control input validation consideration

 I'm using linq to sql to auto generate all these entity classes and data access code. If I want to add data annotation attribute to those auto generated classes, i have to use metadatatype class becuase partial property is not supported. All these classes are called by silverlight client throught WCF. 

 Normally, the code will look like this if we use non partial class.

[Required(ErrorMessage="User Name is required")]
         public string UserName
        {
            get { return _UserName; }
            set
            {
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName="UserName"});
                _UserName = value;
            }
        }
If we use partial class here
 [MetadataType(typeof(UserMetaData))]
    public partial class User
    {
    }

    public class UserMetaData
    {
        [Required(ErrorMessage = "User Name is required.")]
        public string UserName;
    }

My question will be how can i call 
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName="UserName"}); here?>
I'm just confused about these metadatatype and partial class usage here.  
  
  

I can do it!

jessmmy
jessmmy

Member

Member

54 points

64 Posts

Re: User control input validation consideration

I just had another question.

Is System.ComponentModel.DataAnnotations.Validator   only used in silverlight project because I can't find this class from normal c# project?

 

Thanks guys

I can do it!

Mog Liang - MSFT
Mog Lian...

All-Star

All-Star

15908 points

1,549 Posts

Answered Question

Re: User control input validation consideration

Hi,

jessmmy:
My question will be how can i call 
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName="UserName"}); here?>
I'm just confused about these metadatatype and partial class usage here.  

The question I understand, is how to modify WCF generated entities. please check this post

http://silverlight.net/forums/p/114537/262069.aspx#262069

Jessmmy:
Is System.ComponentModel.DataAnnotations.Validator   only used in silverlight project because I can't find this class from normal c# project

If you installed RIA service, then you could find Server-side library containing Validator at

  • [RIA Services install path]\Libraries\Server\System.ComponentModel.DataAnnotations.dll

Thanks,

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities