You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In most scenarios, fields should be validated after the text of each field changes and not an entire form at once after a post attempt. This can already be accomplished with the current ObservableValidator class by inheriting from it for each property. I think the source generators could potentially implement a bit more functionality or the docs could be updated to showcase what is necessary to implement this functionality.
This is how I am currently using the ObservableValidator and I think this would be helpful to others:
First I create a class that implements the typical validation behavior. which can be extended with more functionality.
public partial class InputValidator : ObservableValidator
{
public abstract string Text { get; set; }
[ObservableProperty]
private bool hasValidated;
[ObservableProperty]
private string error;
[RelayCommand]
void Validate()
{
ValidateAllProperties();
HasValidated = true;
if (HasErrors)
Error = string.Join(Environment.NewLine, GetErrors().Select(e => e.ErrorMessage));
else
Error = string.Empty;
}
public void ValidateProps()
{
ValidateAllProperties();
}
}
here is an example how this can be used with a bindable property and in XAML (for MAUI)
public partial class FirstNameValidator : InputValidator
{
private string text;
[ObservableProperty]
[MinLength(2, ErrorMessage = "Name muss mind. 2 Zeichen haben")]
[MaxLength(30, ErrorMessage = "Name darf max. 30 Zeichen haben")]
[Required(ErrorMessage = "Vorname ist Pflichtfeld")]
public override string Text
{
get => text;
set => SetProperty(ref text, value);
}
}
public FirstNameValidator FirstName { get; set; } = new FirstNameValidator();
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
In most scenarios, fields should be validated after the text of each field changes and not an entire form at once after a post attempt. This can already be accomplished with the current ObservableValidator class by inheriting from it for each property. I think the source generators could potentially implement a bit more functionality or the docs could be updated to showcase what is necessary to implement this functionality.
This is how I am currently using the ObservableValidator and I think this would be helpful to others:
First I create a class that implements the typical validation behavior. which can be extended with more functionality.
here is an example how this can be used with a bindable property and in XAML (for MAUI)
Beta Was this translation helpful? Give feedback.
All reactions