title: WPF - How can I validate a new instance of an object?
body: <p>I have this class</p>
  
  <pre><code>public class Doctor
  {
      private string licenseNumber;
      public string LicenseNumber
      {
          get
          {
              return this.licenseNumber;
          }
          set
          {
              if (value.Length &gt; 4)
                  throw new MyException("License Number can't be more then 4 digits.\n");
              if (!new Regex("^[0-9]*$").Match(value).Success) // Allow only numbers
                  throw new MyException("Only numbers are allowed in a License Number.\n");
              this.licenseNumber = value.PadLeft(4,'0'); // Pad with zeros to 4 digits
          }
      }
      private string name;
      public string Name
      {
          get
          {
              return this.name;
          }
          set
          {
              if (!new Regex("^[a-zA-Z ]*$").Match(value).Success) // Allow only letters and spaces 
                  throw new MyException("Only letters and spaces are allowed in a name.\n");
              this.name = value;
          }
      }
      private string phoneNumber;
      public string PhoneNumber
      {
          get
          {
              return this.phoneNumber;
          }
          set
          {
              {
                  if (!new Regex("^0[2-9]-[0-9]{7}$").Match(value).Success) // allow only numbers in format 0[2-9]-xxxxxxx
                      throw new MyException("ERROR: only numbers in format 0[2-9]-xxxxxxx are legal\n");
                  this.phoneNumber = value;
              }
          }
      }
  
  }
  </code></pre>
  
  <p>and much more properties inside it.
  Of course the ctor of this class accepts only form of ctor with values inside, and enter the values to the props.
  Now, I've created a form, which within the user can add a new Doctor.
  Thing is - I want to validate the form before the user click on 'Add'.
  This is a shorted version of my 'add' form in XML:</p>
  
  <pre><code>&lt;Window x:Class="PLForms.AddDoctor"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="AddDoctor" Height="431" Width="381" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:BE;assembly=BE" Loaded="Window_Loaded" Background="White" ResizeMode="CanResizeWithGrip" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Margin="12" HorizontalAlignment="Left" VerticalAlignment="Top"&gt;
  &lt;Window.Resources&gt;
      &lt;CollectionViewSource x:Key="doctorViewSource" d:DesignSource="{d:DesignInstance my:Doctor, CreateList=True}" /&gt;
  &lt;/Window.Resources&gt;
      &lt;Grid Height="367" Name="grid1" Width="296" Margin="12" HorizontalAlignment="Center" VerticalAlignment="Center"&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="*" /&gt;
              &lt;RowDefinition Height="29" /&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Grid DataContext="{StaticResource doctorViewSource}" HorizontalAlignment="Left" Margin="19,13,0,0" Name="grid2" VerticalAlignment="Top"&gt;
              &lt;Label Content="Name:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" /&gt;
              &lt;TextBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3" Name="nameTextBox" Text="{Binding Path=Name, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" /&gt;
              &lt;Label Content="Phone Number:" Grid.Column="0" Grid.Row="3" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" /&gt;
              &lt;TextBox Grid.Column="1" Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="3" Name="phoneNumberTextBox" Text="{Binding Path=PhoneNumber, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" /&gt;
          &lt;/Grid&gt;
          &lt;Button Content="Add" Name="Add" BorderBrush="#FF612355" Foreground="White" Click="Add_Click" Margin="0,326,0,0" Grid.RowSpan="2"&gt;
              &lt;Button.Background&gt;
                  &lt;LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"&gt;
                      &lt;GradientStop Color="Black" Offset="0" /&gt;
                      &lt;GradientStop Color="#85081DDA" Offset="0.893" /&gt;
                  &lt;/LinearGradientBrush&gt;
              &lt;/Button.Background&gt;
          &lt;/Button&gt;
      &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>Now, I think that the validation of data will activated only if I set "DataContext" somehow. I've tried to set a sample Doctor object in the form, assign DataContext to the instance of this object, and then - when the user update the data to illegal one - it activate the validation, and red border is marked around the textbox.
  But I'm looking how to 'activate' validation even if <strong>there's no instance of Doctor before</strong> - when the user got a blank form, fill it by himself, and only then try to click on 'Add'.
  My problem is that in this way, I can't attach DataContext property to anything, since I don't have a Doctor instance, and of course I can't create an empty one, because all of the exceptions I created myself...
  I'll be happy to know what's the logic of <strong>Adding</strong> data to database, and validate it before, not just in update.
  T</p>