|
<sf:SfTextInputLayout HasError="{Binding FirstNameHasError}"
Hint="First Name" ErrorText="Please enter first name">
<sf:SfTextBoxExt
Text="{Binding Path=Model.FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
ValidatesOnNotifyDataErrors=True}" />
</sf:SfTextInputLayout>
<sf:SfTextInputLayout Margin="0,10,0,5"
Hint="Last Name" HasError="{Binding LastNameHasError}" ErrorText="Please enter last name">
<sf:SfTextBoxExt
Text="{Binding Path=Model.FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}" />
</sf:SfTextInputLayout> |
|
public void ValidateData()
{
ClearAllErrors();
var validator = new TestModelValidation();
FluentValidation.Results.ValidationResult result = validator.Validate(Model);
foreach (var error in result.Errors)
{
SetError(error.PropertyName, error.ErrorMessage);
FirstNameHasError = true;
LastNameHasError = true;
}
if (result.IsValid)
{
FirstNameHasError = false;
LastNameHasError = false;
MessageBox.Show("Data good to save !");
}
} |
Hi Dhairya Joshi,
IDataErrorInfo.
[XAML]
<syncfusion:SfTextInputLayout Grid.Row="0" Hint="Name" HelperText="Enter brand name" Margin="0,0,0,10" HasError="{Binding HasErrorCollection[Name]}" ErrorText="{Binding ErrorCollection[Name]}"> <TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" VerticalAlignment="Center" VerticalContentAlignment="Center" /> </syncfusion:SfTextInputLayout> |
[C# | CreateBrandViewModelValidator]
public class CreateBrandViewModelValidator : AbstractValidator<CreateBrandViewModel> { public CreateBrandViewModelValidator() { RuleFor(p => p.Name) .NotEmpty() .MinimumLength(10); RuleFor(p => p.Description) .NotEmpty(); } } |
[C# | CreateBrandViewModel]
public class CreateBrandViewModel : BaseScreenViewModel, IDataErrorInfo { private readonly CreateBrandViewModelValidator _validator; private string _name; private string _description; private Dictionary<string, string> _errorCollection = new Dictionary<string, string>(); private Dictionary<string, bool> _hasErrorCollection = new Dictionary<string, bool>(); public CreateBrandViewModel() { _validator = new CreateBrandViewModelValidator(); } public string Name { get => _name; set { if (value != _name) { _name = value; NotifyOfPropertyChange(() => Name); NotifyOfPropertyChange(() => CanCreate); } } } public string Description { get => _description; set { if (value != _description) { _description = value; NotifyOfPropertyChange(() => Description); NotifyOfPropertyChange(() => CanCreate); } } } public Dictionary<string, string> ErrorCollection { get => _errorCollection; set { if (value != _errorCollection) { _errorCollection = value; NotifyOfPropertyChange(() => ErrorCollection); } } } public Dictionary<string, bool> HasErrorCollection { get => _hasErrorCollection; set { if (value != _hasErrorCollection) { _hasErrorCollection = value; NotifyOfPropertyChange(() => HasErrorCollection); } } } public string Error => null; public string this[string columnName] { get { var validation = _validator.Validate(this, options => { options.IncludeProperties(columnName); }); if (!validation.IsValid) { var failures = validation.Errors; var error = failures .Where(p => p.PropertyName == columnName) .Select(e => e.ErrorMessage) .First(); if (!ErrorCollection.ContainsKey(columnName)) ErrorCollection.Add(columnName, null); if (!HasErrorCollection.ContainsKey(columnName)) HasErrorCollection.Add(columnName, false); ErrorCollection[columnName] = error; HasErrorCollection[columnName] = true; NotifyOfPropertyChange(() => ErrorCollection); NotifyOfPropertyChange(() => HasErrorCollection); return error; } else { ErrorCollection[columnName] = null; HasErrorCollection[columnName] = false; NotifyOfPropertyChange(() => HasErrorCollection); NotifyOfPropertyChange(() => ErrorCollection); } return null; } } } |