bodong1987 / Avalonia.PropertyGrid

A property edit control in Avalonia like DevExpress's PropertyGridControl.
https://www.cnblogs.com/bodong
MIT License
187 stars 18 forks source link

Root object is not accessible in ValidationContext #21

Closed michalczerwinski closed 7 months ago

michalczerwinski commented 9 months ago

When using custom ValidationAttribute it is only possible to access property value. This makes impossible creating validation which relies on multiple fields.

Potential solutions:

  1. Add root object into items dictionary in ValidationContext:
new ValidationContext(value, new Dictionary<object, object> { {"root", component} })
                        {
                            DisplayName = property.DisplayName,
                            MemberName = property.Name
                        },
  1. Extending ValidatorUtils.TryValidateObject to support IValidatableObject interface.

I can prepare pull request if you would be willing to accept it. What do you think?

bodong1987 commented 8 months ago

When using custom ValidationAttribute it is only possible to access property value. This makes impossible creating validation which relies on multiple fields.

Potential solutions:

  1. Add root object into items dictionary in ValidationContext:
new ValidationContext(value, new Dictionary<object, object> { {"root", component} })
                        {
                            DisplayName = property.DisplayName,
                            MemberName = property.Name
                        },
  1. Extending ValidatorUtils.TryValidateObject to support IValidatableObject interface.

I can prepare pull request if you would be willing to accept it. What do you think?

My initial idea is that if you rely on multiple conditions, you can add a separate attribute specifically to support this situation, as long as this attribute is invisible to the control. There are also some logical problems when supporting multiple conditions, such as are these conditions and or or?

I very much welcome all kinds of pull requests. After all, a person's energy and knowledge are limited, as long as it can run correctly. grateful.