Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to provide validation for child classes that implement different properties?
    primarykey
    data
    text
    <p>I have a parent class implementing INotifyPropertyChanged, and the parent class has multiple children. The children have different properties that all call PropertyChanged. I want to add validation, but I really don't want to have to write validations for every child class. The validation rules are supplied from the database, so I would have to eventually pull the validation rules for each child, then check the value against the rules. If I did that, I think that it would have too much redundant code, and I would like to have it placed at the parent level since PropertyChanged triggers on the string value of the value itself. </p> <p>Is it possible to have the validation method on the parent class so I wouldn't have to write a validation method for every child class? Mind you, the properties in every child class are different.</p> <p>Below is what I currently have, with validation in the child class.</p> <pre><code>public Parent : INotifyChanged { /// &lt;summary&gt; /// Occurs when a property is changed /// &lt;/summary&gt; public event PropertyChangedEventHandler PropertyChanged; /// &lt;summary&gt; /// Raises the &lt;see cref="PropertyChanged"/&gt; for a given /// property. /// &lt;/summary&gt; /// &lt;param name="propertyName"&gt;&lt;/param&gt; protected void OnPropertyChanged(String propertyName) { // Get the hanlder PropertyChangedEventHandler handler = this.PropertyChanged; // Check that the event handler is not null if(null != handler) { // Fire the event handler(this, new PropertyChangedEventArgs(propertyName)); } } } </code></pre> <p>Child1 Class:</p> <pre><code>public Child1 : Parent, IDataErrorInfo { private Dictionary&lt;string, string&gt; m_validationErrors = new Dictionary&lt;string, string&gt;(); private void Validate() { this.RemoveError("Child1Description"); if(!Regex.IsMatch(Child1Description, "^([a-zA-Z '-]+)$") &amp;&amp; !String.IsNullOrWhiteSpace(Description)) { this.AddError("Child1Description", "Only non-numerics allowed."); } } private void AddError(string columnName, string msg) { if(!m_validationErrors.ContainsKey(columnName)) { m_validationErrors.Add(columnName, msg); } } private void RemoveError(string columnName) { if(m_validationErrors.ContainsKey(columnName)) { m_validationErrors.Remove(columnName); } } public string Error { get { if(m_validationErrors.Count &gt; 0) { return "Field data is invalid."; } else return null; } } public string this[string columnName] { get { if(m_validationErrors.ContainsKey(columnName)) { return m_validationErrors[columnName]; } else { return null; } } } /// &lt;summary&gt; /// Description of the air entity /// &lt;/summary&gt; public string Child1Description { get { return Child1description; } set { description = value; Validate(); OnPropertyChanged("Child1Description"); } } } </code></pre> <p>Child2 Class:</p> <pre><code>public Child2 : Parent, IDataErrorInfo { private Dictionary&lt;string, string&gt; m_validationErrors = new Dictionary&lt;string, string&gt;(); private void Validate() { this.RemoveError("Child2Description"); if(!Regex.IsMatch(Child2Description, "^([a-zA-Z '-]+)$") &amp;&amp; !String.IsNullOrWhiteSpace(Description)) { this.AddError("Child2Description", "Only non-numerics allowed."); } } private void AddError(string columnName, string msg) { if(!m_validationErrors.ContainsKey(columnName)) { m_validationErrors.Add(columnName, msg); } } private void RemoveError(string columnName) { if(m_validationErrors.ContainsKey(columnName)) { m_validationErrors.Remove(columnName); } } public string Error { get { if(m_validationErrors.Count &gt; 0) { return "Field data is invalid."; } else return null; } } public string this[string columnName] { get { if(m_validationErrors.ContainsKey(columnName)) { return m_validationErrors[columnName]; } else { return null; } } } /// &lt;summary&gt; /// Description of the air entity /// &lt;/summary&gt; public string Child2Description { get { return Child2description; } set { description = value; Validate(); OnPropertyChanged("Child2Description"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload