C
C#13mo ago
RV

❔ How to check Validation Error programically C# in MVVM?

No description
10 Replies
RedBear
RedBear13mo ago
To check validation errors programmatically in C# when using the Model-View-ViewModel (MVVM) pattern, you would typically implement the INotifyDataErrorInfo interface for your ViewModel or Model classes. This interface allows you to provide asynchronous validation support and works well with data binding in WPF applications. Here's a basic outline of the steps you would follow: Implement INotifyDataErrorInfo: Your ViewModel or Model should implement the INotifyDataErrorInfo interface, which requires you to define an event named ErrorsChanged and methods like GetErrors and HasErrors. Validate Properties: Within the property setters of your ViewModel or Model, you would perform the validation checks and add any errors to a collection that keeps track of current validation issues. Notify Validation Errors: When validation errors occur, you should raise the ErrorsChanged event to notify the UI that there are new validation errors to be displayed. Check Validation Errors Programmatically: You can check for validation errors programmatically by accessing the HasErrors property or by calling the GetErrors method and inspecting the returned values.
RedBear
RedBear13mo ago
No description
RedBear
RedBear13mo ago
The XAML bindings and ValidationRules define how the errors are displayed, but they do not, by themselves, provide a method to check for errors programmatically. To do that, you would typically have to go through the ViewModel or the code-behind.
RV
RV13mo ago
I'm still stuck when initializing usercontrol, the error is not validated so the button can be pressed. But after filling it in and then emptying it, it just updated.
No description
No description
RV
RV13mo ago
Here is my code
RV
RV13mo ago
AddCategoryVM.cs
public class AddCategoryVM : PopupModelBase
{
public AddCategoryVM() : base()
{
}

private string _categoryName;

public string CategoryName
{
get
{
return _categoryName;
}
set
{
ClearErrors();
var result = new TextNotEmptyValidation() { FieldName = "Nama Kategori", CustomErrorMessage = ValidationMessages.RequiredError }.Validate(value, CultureInfo.CurrentCulture);
if (!result.IsValid) AddError(result.ErrorContent as string);
_categoryName = value;
OnPropertyChanged();
}
}
}
public class AddCategoryVM : PopupModelBase
{
public AddCategoryVM() : base()
{
}

private string _categoryName;

public string CategoryName
{
get
{
return _categoryName;
}
set
{
ClearErrors();
var result = new TextNotEmptyValidation() { FieldName = "Nama Kategori", CustomErrorMessage = ValidationMessages.RequiredError }.Validate(value, CultureInfo.CurrentCulture);
if (!result.IsValid) AddError(result.ErrorContent as string);
_categoryName = value;
OnPropertyChanged();
}
}
}
RV
RV13mo ago
And when using the number property, I can't change the default error message
No description
RV
RV13mo ago
@RedBear
Florian Voß
Florian Voß13mo ago
@RedBear pls dont use AI to help people here
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server