Skip to main content

Microsoft Silverlight

Unanswered Question Dataform Data ValidationRSS Feed

(0)

ilya.shmorgun
ilya.shm...

Member

Member

4 points

91 Posts

Dataform Data Validation

Hi,

I just started experimenting with the Dataform control and I have created validation characteristics for that when the data doesn't meet the required style a notification pops up at the bottom of the form. However, what I really am trying to figure out is how to show a red bounding box around the particular field, where the data doesn't match the validation. Do I have to specify a separate property for that?


---------------------------------

Microsoft Student Partner

http://thund3rcat.spaces.live.com

Luke Longley
Luke Lon...

Member

Member

465 points

92 Posts

Re: Dataform Data Validation

Does your data class throw exceptions in their property setters when the value passed in is invalid?  It needs to do so in order to get that behavior - the red bounding box in the invalid state is something built into the stock Silverlight controls (not the DataForm), and is triggered when BindingValidationError is fired on one of those controls.

ilya.shmorgun
ilya.shm...

Member

Member

4 points

91 Posts

Re: Re: Dataform Data Validation

I've created my Dataform and a Person class, just like in the tutorial here: http://silverlight.net/learn/learnvideo.aspx?video=187317

However when I tryed to hook up exceptions to the properties, I was always getting a debug error. 

Maybe you could help write an exception for FirstName property, so that when the user leaves it blank and hits Save, a red bounding box appears. 

Here's my code:

[Display(Name = "Eesnimi", Description = "Sisesta oma eesnimi")]
[Required(ErrorMessage = "Palun sisesta eesnimi")]
public string FirstName{ get; set; }
 


---------------------------------

Microsoft Student Partner

http://thund3rcat.spaces.live.com

Luke Longley
Luke Lon...

Member

Member

465 points

92 Posts

Re: Re: Dataform Data Validation

You can actually use a new static class in System.ComponentModel.DataAnnotations called Validator to have it automatically parse the validation attributes and throw an exception when the rules they specify are not met.  To get that, you would need to do this:

using System.ComponentModel.DataAnnotations;
// Then, within the class...
private string _firstName;
[Display(Name = "Eesnimi", Description = "Sisesta oma eesnimi")]
[Required(ErrorMessage = "Palun sisesta eesnimi")]
public string FirstName
{
get
{
return this._firstName;
}
set
{
if (value != this._firstName)
{
this.ValidateProperty("FirstName");
this._firstName = value;
}
}
}
private void ValidateProperty(string propertyName)
{
ValidationContext validationContext = new ValidationContext(this, null, null);
validationContext.MemberName = propertyName;
Validator.ValidateProperty(this, validationContext);
}

That should make it throw an exception whenever the value is blank (since the Required attribute has been used).  Let me know if this works for you.

ilya.shmorgun
ilya.shm...

Member

Member

4 points

91 Posts

Re: Re: Re: Dataform Data Validation

I've tryed your code, and this in an error I'm receiving after building the solution. 

ArgumentException Is this the way it's supposed to be, or am I doing something wrong?


---------------------------------

Microsoft Student Partner

http://thund3rcat.spaces.live.com

Luke Longley
Luke Lon...

Member

Member

465 points

92 Posts

Re: Re: Re: Dataform Data Validation

Oops, sincere apologies... that's what I get for giving you code without running it myself. :)

This should work:

using System.ComponentModel.DataAnnotations;

// Then, within the class...

private string _firstName;

[Display(Name = "Eesnimi", Description = "Sisesta oma eesnimi")]
[Required(ErrorMessage = "Palun sisesta eesnimi")]
public string FirstName
{
get
{
return this._firstName;
}
set
{
if (value != this._firstName)
{
this.ValidateProperty(value, "FirstName");
this._firstName = value;
}
}
}

private void ValidateProperty(object value, string propertyName)
{
ValidationContext validationContext = new ValidationContext(this, null, null);
validationContext.MemberName = propertyName;
Validator.ValidateProperty(value, validationContext);
}

I had accidentally passed the object itself into Validator.ValidateProperty() instead of the value to validate against.  Sorry about that.

ilya.shmorgun
ilya.shm...

Member

Member

4 points

91 Posts

Re: Re: Re: Re: Dataform Data Validation

Would it be possible to make the ValidateProperty method accept other types, besides string? Integer for example?


---------------------------------

Microsoft Student Partner

http://thund3rcat.spaces.live.com

Luke Longley
Luke Lon...

Member

Member

465 points

92 Posts

Re: Re: Re: Re: Dataform Data Validation

It can actually accept any type you want - the string parameter to the method is just to pass in the property name, whereas the object parameter is where the value itself gets passed.  The reason why the error message you saw complained that it wasn't a string was just because the property being validated was a string, not because the value must always be a string.  If it had been (say) an int, it would have complained that it wasn't an int instead.

ilya.shmorgun
ilya.shm...

Member

Member

4 points

91 Posts

Re: Re: Re: Re: Re: Dataform Data Validation

Ok, now there's a different kind of exception I'm getting. This stuff seems to be harder, than it should be..

 


---------------------------------

Microsoft Student Partner

http://thund3rcat.spaces.live.com

Luke Longley
Luke Lon...

Member

Member

465 points

92 Posts

Re: Re: Re: Re: Re: Re: Dataform Data Validation

That exception is actually completely expected - raising exceptions in a setter is how a validation error is raised.  If you click "continue" in the debugger, your program should continue normally and you should get the validation error you were wanting.

ilya.shmorgun
ilya.shm...

Member

Member

4 points

91 Posts

Re: Re: Re: Re: Re: Re: Re: Dataform Data Validation

When I click continue it shows me an empty form with no fields or buttons in it. Maybe it is cause by the fact that I'm using the AutoGenerateFields="true" property?


---------------------------------

Microsoft Student Partner

http://thund3rcat.spaces.live.com

Luke Longley
Luke Lon...

Member

Member

465 points

92 Posts

Re: Re: Re: Re: Re: Re: Re: Dataform Data Validation

It might be useful to see the XAML where the DataForm is defined - can you post that?

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities