Trouble validating data in Silverlight 4 RIA services
I have a Silverlight 4 RIA services project that I'm working on and I'm unable to validate a Child Windows text input. I have a text box such as this one:
<TextBox Height="23" Name="txtSummary" Width="Auto" Grid.Row="2" Grid.Column="4" Text="{Binding DocumentView.Summary, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"/>
The DocumentView.Summary value is obtained from my View Mode开发者_运维问答l:
public DocumentSubmittedView DocumentView
{
get { return _DocumentView; }
set
{
if (_DocumentView != value)
{
_DocumentView = value;
OnPropertyChanged("DocumentView");
}
}
}
And DocumentSubmittedView is its own Custom Entity class:
namespace Data.Model.Entities {
public class DocumentSubmittedView {
[Key]
public int DocID { get; set; }
[Required(ErrorMessage = "Summary Required")]
public string Summary { get; set; }
}
}
Is there any reason why I'm unable to get any type of Error Validation message with the above information?
Thanks.
If you're using custom entites, which are not generated my entity framework or Linq2Sql, then add validation logic to its setters, like this:
[Range(0,1000)]
[DataMember]
public int Population
{
get { return _population; }
set {
Validator.ValidateProperty(value,
new ValidationContext(this, null, null)
{ MemberName = "Population" });
_population = value;
RaisePropertyChanged("Population");
}
}
and binding flags on TextBox element inside your XAML file:
<TextBox Text="{Binding Population,
Mode=TwoWay, NotifyOnValidationError=True,ValidatesOnExceptions=True}" />
From MSDN :
Currently, the DataGrid control is the only control that automatically applies validation attributes. For an example of using a DataGrid control with a class that contains validation attributes, see the Data Grid Example below. When you do not use the DataGrid control, you must manually validate the values.
If you aren't using a DataGrid
Manually Validating Values
When you do not use the DataGrid control to provide the interface for editing data, the validation attributes are not automatically applied. However, you can manually apply the validation test by using the Validator class. You can call the ValidateProperty method on the set accessor of a property to check the value against the validation attributes for the property. You must also set both ValidatesOnExceptions and NotifyOnValidationError properties to true when data binding to receive validation exceptions from validation attributes. For an example of manually applying validation, see the Data Binding Example below.
Hi I am not seeing the complete code but...
could it be that on creation of the object DocumentSubmittedView
the Summary is null.
The OnPropertyChanged
get only fired when Summary changes... since you have not entererd anything there is no reason to change. But if you enter something you provided a Summary and then your validatin rule is positive and there is no reason for a validation error.
What you can do is enter some text in the Summary and then delete it...
And now you can see if your rule works...
but this is just from the top of my head be seeing your few lines..
It can help to assign string.Empty to the Summary property in the constructor of the viewmodel HTH
精彩评论