Open tuyen-vuduc opened 2 years ago
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(PhoneNumberValid), nameof(PhoneNumberInvalidMessage))]
[Required(ErrorMessage = "Your phone number is required to recover your password.")]
[Phone(ErrorMessage = "You have enter an invalid phone number.")]
string phoneNumber;
It seems you didn't add [NotifyDataErrorInfo]
, doesn't that solve the issue already?
The generator doesn't generate validation code by default, that's by design, it's opt-in 🙂
@Sergio0694 Thanks for your suggestion.
By using [NotifyDataErrorInfo]
, we don't need partial void On[PropertyName]Changing(string value)
implementation any more.
However, if there is a way to add other properties as well, it will really save time and effort. (I am a bit old guy of web validation where I want to validate inputs individually as well as the form as a whole)
The other point, if we can make use of SetProperty
in ObservableObject or ObservableValidator, we can shorten the generated code.
How do you think?
If you look carefully, my suggestion brings in new methods to do the web form like validation,
public abstract class BaseFormModel : ObservableValidator
{
protected virtual string[] ValidatableAndSupportPropertyNames => new string[0];
public virtual bool IsValid()
{
ValidateAllProperties();
foreach (var propertyName in ValidatableAndSupportPropertyNames)
{
OnPropertyChanged(propertyName);
}
return !HasErrors;
}
}
If we can bring them into ObservableValidator
class and generate ValidatableAndSupportPropertyNames
as the subclass, it'll really help.
Overview
I tried to add validations for my sign in and sign forms based on ObservableValidator.
I found that there are many lines of code can be generated by a generator.
Here is a snapshot of my code to have validation in my form
Another version
API breakdown
Add
IsValid
toObservableValidator
classChanges to the generated observable property
SetProperty
instead of assigning backing field directly if the base class isObservableObject
SetProperty
version ofObservableValidator
if the owning class inherits from that operation with paramvalidate
assigned to true if there is any validation attributesCurrent generated code
Base class is ObjectValidator
Base class is ObservableObject
Usage example
Breaking change?
No
Alternatives
Define properties manually
Another version
Additional context
No response
Help us help you
Yes, I'd like to be assigned to work on this item