Straight forward and highly configurable validation for WPF application.
{
"viewConfigs": [
{
"name": "Login",
"fields": [
{
"name": "Username",
"isRequired": true,
"maxLength": 50
},
{
"name": "Password",
"isRequired": true
}
]
}
]
}
ViewConfig = await ValidationContext.LoadViewConfigAsync("Login")
ViewConfig.AddCustomValidator(
fieldName: "Password",
validate: o => _membershipService.Login(Username, Password),
messageKey: "InvalidUsernamePassword");
ui:ViewValidatorControl ViewConfig="{Binding ViewConfig}"/>`
var result = validationContext.Validate(ViewConfig, this);
if (result.IsValid)
{
//...
}
<ItemsControl ItemsSource="{Binding ValidationMessages}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DockPanel>
<Image Height="12" Source="{StaticResource Icon:Error}"/>
<TextBlock Text="{Binding Message}" Foreground="Red"/>
</DockPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<sys:String x:Key="InvalidUsernamePassword">Username is incorrect or the password does not match!</sys:String>
<sys:String x:Key="InvalidEmail">That does not appear to be a real email address!</sys:String>
public class MyAppMessageProvider : IMessageProvider
{
//...
}
//no code needed!
public class MyAppConfigProvider : IConfigProvider
{
//...
}
ValidationContext.Global.ValidationFailCommand = new HandleValidationFailCommand(); //Custom command
var result = ValidationContext.Validate(ViewConfig, this);
if (!result.IsValid)
{
ValidationMessages.AddRange(result.Messages);
}
- Required field
- Number range
- Date range
- String length (min and max)
- Regex
- Custom - everything else you need