title: Notify EditContext that field has changed for Blazor validation
body: <p>I am trying to understand the inner workings of Blazor (and eventually write some middleware).  I have a <a href="https://blazorfiddle.com/s/niol9ihi" rel="nofollow noreferrer">fiddle</a> that binds three different fields in three different ways:</p>
  <p><a href="https://i.stack.imgur.com/kwHXL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kwHXL.png" alt="fiddle" /></a></p>
  <ul>
  <li>The FirstName field is bound to an InputText works as expected and displays the validation message when clearing the box and focus changes.</li>
  <li>The MiddleName field is bound to a regular input and does not work &quot;as expected&quot; and displays no validation message when it is cleared and focus changes.  By going thru the Blazor source, I've identified that EditContext.NotifyFieldChanged is a concern if the control itself (InputBase).
  <ul>
  <li>Worth noting that the message IS displayed if you submit the form which I would like to understand the lifecycle involved.</li>
  </ul>
  </li>
  <li>The LastName field is also bound to a regular input, but does a bit of hackery to raise EditContext.OnFieldChanged and trigger the validation to work.  <strong>Is there a better way to do this for onchange?  How does the submit manage to raise the validation message?</strong>  There is a lot of reference capture, expressions, reflection, and use of the &quot;internal use only&quot; CreateBinder method.</li>
  </ul>
  <p>Below is the code for the fiddle for easier reference:</p>
  <pre><code>@page &quot;/&quot;
  @using System.ComponentModel.DataAnnotations
  @using System.Linq.Expressions;
  @using System.Reflection;
  @implements IHasEditContext;
  
  &lt;h1&gt;Hello, world!&lt;/h1&gt;
  
  &lt;EditForm EditContext=&quot;this.EditContextRef&quot;&gt;
      &lt;DataAnnotationsValidator&gt;&lt;/DataAnnotationsValidator&gt;
      &lt;div class=&quot;form-group&quot;&gt;
          &lt;InputText @bind-Value=&quot;@this.FirstName&quot; class=&quot;form-control&quot; /&gt;
          &lt;ValidationMessage For=&quot;() =&gt; this.FirstName&quot;&gt;&lt;/ValidationMessage&gt;
      &lt;/div&gt;
      &lt;div class=&quot;form-group&quot;&gt;
          &lt;input @bind-value=&quot;@this.MiddleName&quot; class=&quot;form-control&quot; /&gt;
          &lt;ValidationMessage For=&quot;() =&gt; this.MiddleName&quot;&gt;&lt;/ValidationMessage&gt;
      &lt;/div&gt;
      &lt;div class=&quot;form-group&quot;&gt;
          &lt;input value=&quot;@this.LastName&quot; class=&quot;form-control&quot; @onchange=&quot;(CreateBinder2(this, () =&gt; this.LastName,  this.LastName))&quot; /&gt;
          &lt;ValidationMessage For=&quot;() =&gt; this.LastName&quot;&gt;&lt;/ValidationMessage&gt;
      &lt;/div&gt;
  
  
      &lt;input type=&quot;submit&quot; value=&quot;Go&quot; /&gt;
  &lt;/EditForm&gt;
  
  @code {
      protected override void OnInitialized()
      {
          base.OnInitialized();
          EditContextRef = new EditContext(this);
      }
  
      //BasicFormValidator Form1Validator = new BasicFormValidator();
      [Required]
      public String FirstName { get; set; } = &quot;delete me and change focus to cause validation&quot;;
      [Required]
      public String MiddleName { get; set; } = &quot;delete me and change focus - no validation&quot;;
      [Required]
      public String LastName { get; set; } = &quot;delete me and change focus - validation but manually calling NotifyFieldChanged&quot;;
  
      public EditContext EditContextRef { get; set; }
  
      public static EventCallback&lt;ChangeEventArgs&gt; CreateBinder2(
          IHasEditContext receiver,
          Expression&lt;Func&lt;string?&gt;&gt; propExpression,
          string existingValue,
          System.Globalization.CultureInfo? culture = null)
      {
          var fieldIdentifier = FieldIdentifier.Create(propExpression);
          Action&lt;String&gt; valueSetter = (string v) =&gt;
          {
              PropertyInfo prop = fieldIdentifier.Model.GetType().GetProperty(fieldIdentifier.FieldName, BindingFlags.Public | BindingFlags.Instance);
              prop.SetValue(fieldIdentifier.Model, v);
              receiver.EditContextRef.NotifyFieldChanged(fieldIdentifier);
          };
  
          return EventCallback.Factory.CreateBinder&lt;string&gt;(receiver, valueSetter, existingValue, culture);
      }
  
  
  }
  </code></pre>