Validation reloads data when validation fails. How to switch this off?

Posts   
 
    
thilker
User
Posts: 7
Joined: 16-Feb-2006
# Posted on: 20-Oct-2006 17:56:29   

Hi!

I'm using an EntityCollection as Datasource. A bindingsource is connected to it. To the bindingsource some textboxes are bound. Ok. Thats quiet usual.

In the entity class I validate the input and if an error occurs I use "SetEntityFieldError(...)" to describe the error.

In the form I bind the IDataErrorInfo to the Errorprovider. No problems so far.

BUT

when I start the application and leave a textbox with a value that causes the validation to fail, the old value is reloaded from the collection and appears in the field. The erroricon is still shown. What I want is, that the user sees his wrong entry and correct it.

What is the trick to switch off this "reloading" mechanism?

Regards and thanks in advance!

Thorsten

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 21-Oct-2006 03:02:00   

Can you post your validation logic? What it sounds like is happening is that when you set the value of a property for an entity validation occurs. If the value is invalid it is rejected and not set. If this happens then the entity never has that value and so the textbox when reflects the value originally in the entity.

thilker
User
Posts: 7
Joined: 16-Feb-2006
# Posted on: 23-Oct-2006 10:10:20   

Hi!

Here is my validation code. (I've created a partial class for it)


public partial class MailingTemplateEntity : EntityBase2, ISerializable
    {
        protected override bool OnValidateFieldValue(int fieldIndex, object value)
        {
            bool toReturn = true;
            switch ((MailingTemplateFieldIndex)fieldIndex)
            {
                case MailingTemplateFieldIndex.Bezeichnung:
                {
                    string text = (string)value;
                    if (text == null || text == "")
                    {
                        SetEntityFieldError(MailingTemplateFieldIndex.Bezeichnung.ToString(), "Die Bezeichnung darf nicht leer sein.", false);
                        toReturn = false;
                    }
                    break;
                }

                case MailingTemplateFieldIndex.Betreff:
                {
                    string text = (string)value;
                    if (text == null || text == "")
                    {
                        SetEntityFieldError(MailingTemplateFieldIndex.Betreff.ToString(), "Der Betreff darf nicht leer sein.", false);
                        toReturn = false;
                    }
                    break;
                }

                case MailingTemplateFieldIndex.Text:
                {
                    string text = (string)value;
                    if (text == null || text == "")
                    {
                        SetEntityFieldError(MailingTemplateFieldIndex.Text.ToString(), "Der Text darf nicht leer sein.", false);
                        toReturn = false;
                    }
                    break;
                }

                case MailingTemplateFieldIndex.TemplatekategorieId:
                {
                    if (value == null)
                    {
                        SetEntityFieldError(MailingTemplateFieldIndex.Text.ToString(), "Die Kategorie darf nicht leer sein.", false);
                        toReturn = false;
                    }
                    break;
                }

                default:
                {
                    toReturn = true;
                    break;
                }
            }
            return toReturn;
        }
    }   

In the form I added the following code to display the error message

(Bound to the event "Validated" of the textboxes):

       private void control_Validated(object sender, EventArgs e)
        {
            // Pointer auf Errormeldungen aus dem BO
            IDataErrorInfo dataErrorInfo = ((IDataErrorInfo)_templateBindingSource.Current);

            // Pointer auf das Control
            Control control = (Control)sender;

            // Ist das Control überhaupt (schon) gebunden?
            if (control.DataBindings.Count > 0)
            {   
                // Hänge die Errormeldung des BO-Feldes an das entsprechende Control
                this._myError.SetError(
                    control, dataErrorInfo[control.DataBindings[0].BindingMemberInfo.BindingField]);
            }
        }

Regards Thorsten

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 24-Oct-2006 05:41:15   

So are you blanking out the field and then when you save it is reset to the original value? If this is not the desired behavior I would look at implementing on Save validation so that the values can be set for the properties that are databound.

thilker
User
Posts: 7
Joined: 16-Feb-2006
# Posted on: 24-Oct-2006 10:27:28   

For me the following behaviour would be optimal:

If a user has entered something invalid and has tabbed to the next field, the error-icon with the message should appear. The invalid value should not be replaced by the previous valid value.

The Dataset can not be saved as long as a field has an invalid value.

The user has to correct the field before he is able to save the data.

I think this should be the normal behaviour of an input form or? Otherwise "by field" validation would make not many sense.

Regards, Thorsten

Walaa avatar
Walaa
Support Team
Posts: 14954
Joined: 21-Aug-2005
# Posted on: 24-Oct-2006 10:35:25   

The Dataset can not be saved as long as a field has an invalid value.

If you have a validation upon a field, which rejects some value to be set for the field for some reason. Therefore the field can not have an invalid value (it was never set).

The behaviour you are looking for can be implemented using a "validation control" over the TextBox in hand. With the Save button "Causes validation" property set to true.

thilker
User
Posts: 7
Joined: 16-Feb-2006
# Posted on: 24-Oct-2006 10:49:18   

That sounds interessting. So I was doing my validation on the wrong layer. I will try your proposal.

Regards, Thorsten