in

CodeSmith Community

Your Code. Your Way. Faster!

Entity Validation - Empty Strings

Last post 12-13-2006 3:33 PM by Badger. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 12-11-2006 5:21 PM

    • Badger
    • Not Ranked
    • Joined on 12-11-2006
    • Posts 6
    • Points 90

    Entity Validation - Empty Strings

    Hi Nettiers,

    First great job - steep learning curve for us junior programmers but - great job. It has actually given me insight into how many of the .NET framework interfaces should be used.

    However, I am struggling to understand why the validation for empty strings doesn't seem to work in my bound winform DataGridView control. I am using the latest SVN trunk code and have changed my EntityInstanceBase.generated.cst template to include the following in the AddValidationRules method:

     if (! cols[x].AllowDBNull && IsCSReferenceDataType(cols[x]))
       {%>
       ValidationRules.AddRule(Validation.CommonRules.NotNull,"<%=propertyName%>");
                ValidationRules.AddRule(Validation.CommonRules.StringRequired,"<%=propertyName%>");
       <%
       }

    After generation my entities include this new validation rule.

    I have a number of basic settings forms with DataGridViews bound to basicSettingXService.GetAll() .

    On adding a new item I hijack the bindingSource addingNew event and have the option of setting a default for the basicSetting object description. If I set it to null the DataDridView reports on the invalid null state of the description property. BUT an empty string in the same property is mysteriously valid.  See form code below.

    I have read quite a few different notes on this in the forums and although the same issue repeated gets described it is passed off as programmer error. Given the simplicity of the example below I cannot see why the validation for StringNotNullOrEmpty fails.

     //WEIGHT UNIT FORM 

    using System;
    using System.ComponentModel;
    using System.Windows.Forms;

    using Dal.Component;
    using Dal.Entities;

    namespace App
    {

    public partial class FormWeightUnit : Form

    {

    //Create a new Service Workspace object to work with;
    WeightUnitService weightUnitService = new WeightUnitService();

     

    // Fields
    private static FormWeightUnit instance;

    protected FormWeightUnit(Form form)
    {
    InitializeComponent();
    MdiParent = form;
    }

    // Methods
    public static FormWeightUnit Instance(Form form)
    {
    // Uses "Lazy initialization"
    if( instance == null )
    {
    instance =
    new FormWeightUnit(form);
    }

    return instance;

    }

    private void FormWeightUnit_Load(object sender, EventArgs e)
    {
    LoadData();
    }

    private void LoadData()
    {
    weightUnitBindingSource.DataSource = weightUnitService.GetAll();
    weightUnitDataGridView.AutoGenerateColumns =
    true;
    weightUnitDataGridView.Columns[
    "ID"].Visible = false;
    }

    private void SaveAll()
    {
    TList<WeightUnit> list = weightUnitBindingSource.DataSource as TList<WeightUnit>; 

    if(list.IsValid)
    {
    weightUnitService.Save(list);
    }

    }

    private void weightUnitBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
    SaveAll();
    }

    private void FormWeightUnit_FormClosed(object sender, FormClosedEventArgs e)
    {
    instance =
    null;
    }

    private void weightUnitBindingSource_AddingNew(object sender, AddingNewEventArgs e)
    {

    //A WeightUnit consists of an ID identity and a string Description
    WeightUnit w = new WeightUnit();
    //w.Description = null;
    e.NewObject = w;
    }

    }

    }

    • Post Points: 5
  • 12-12-2006 12:50 PM In reply to

    • Badger
    • Not Ranked
    • Joined on 12-11-2006
    • Posts 6
    • Points 90

    Re: Entity Validation - Empty Strings

    Can I supply any further information to assist in the diagnosis?

    This problem has been appearing and discussed in the forums since january this year. Surely someone must have come up with a solution?

    Regards,

    Badger.

    • Post Points: 35
  • 12-13-2006 2:56 AM In reply to

    Re: Entity Validation - Empty Strings

    Hi

     I believe the validation code doesn't get called until either you specifically call it (via Validate()) or when you try to save the Entity so perhaps that's why.  Try stepping through the code to see if it's getting called (I have a feeling you'll find that the validation isn't being called at that point).

    HTH

    Martin

    • Post Points: 35
  • 12-13-2006 2:58 PM In reply to

    • Badger
    • Not Ranked
    • Joined on 12-11-2006
    • Posts 6
    • Points 90

    Re: Entity Validation - Empty Strings

    pritcham:

     I believe the validation code doesn't get called until either you specifically call it (via Validate()) or when you try to save the Entity so perhaps that's why.  Try stepping through the code to see if it's getting called (I have a feeling you'll find that the validation isn't being called at that point).

     

    Thanks for the input Martin. I'll double check but all the other validation rules fire for the entity when I load an entity and assign it with defaults. These errors are shown when the entity is displayed in a datagrid via a binding source + error provider. All validate on property change except for StringNotNullOrEmpty.

    ?????

    • Post Points: 5
  • 12-13-2006 3:33 PM In reply to

    • Badger
    • Not Ranked
    • Joined on 12-11-2006
    • Posts 6
    • Points 90

    Re: Entity Validation - Empty Strings

    Answer

    My humble apologies to all. I have spent more time debugging as suggested by Martin and found the cause of the problem:

    public static bool StringRequired(object target, ValidationRuleArgs e)
    {

    PropertyInfo p = target.GetType().GetProperty(e.PropertyName);
    if (p != null)
    {

    string value = (string)p.GetValue(target,null);


    if (!(value == null)) value = value.Trim();
     

    if (string.IsNullOrEmpty(value))
    {

    if (string.IsNullOrEmpty(e.Description)) e.Description = e.PropertyName + " required";
    return false;

    }
    return true;


    }
    else
    {

    throw new ArgumentException(string.Format("Property \"{0}\" not found on object \"{1}\"",e.PropertyName,target.GetType().ToString()));

    }

    }

    The original validation method in Entities.Validation.CommonRules is designed to check ONLY for string empty or null and not blank string. In the code I added a line (in bold) and now my 'empty' strings are invalid. I will write a separate rule for this validation and add it to my templates.

     Thanks for your feedack.

    • Post Points: 5
Page 1 of 1 (5 items)
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems