Corovei Andrei
Corovei Andrei

Reputation: 1668

Invalidate control on Save

I have a WPF form that has some controls. One of the controls is a text box that specifies an string propertyenter image description here

defined by:

        <TextBox>
            <TextBox.Text>
                <Binding Path="ExtractName" UpdateSourceTrigger="LostFocus">
                    <Binding.ValidationRules>
                        <ExceptionValidationRule />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

The binded property is defined as:

    private string _extractName;
    public string ExtractName
    {
        get { return _extractName; }
        set
        {
            var extract_id = (int?) null;
            if(SelectedExtract != null)
            {
                extract_id = SelectedExtract.ExtractId;
            }

            if(SelectedExtract == null)
            if (WebServiceCall.ExtractNameExists(extract_id, value))
            {
                _isExtractNameValid = false;
                throw new ApplicationException("Extract Name already exists");
            }

            if (value == "")
            {
                _isExtractNameValid = false;
                throw new ApplicationException("Extract Name cannot be empty");
            }

            _extractName = value;
            _isExtractNameValid = true;
            RaisePropertyChanged("ExtractName");
        }
    }

This works just fine if the user enters the control and writes something in it.

enter image description here

But, when a call to the save method is made, I want the control to check all validations and if something is wrong, the text box should be colored red.

How can I do this from the view model?

Upvotes: 4

Views: 151

Answers (2)

MrWombat
MrWombat

Reputation: 649

You could implement IDataErrorInfo providing a collection of validation results. That would be easier to check before/while save.

You could also do a very dirty solution: Set all properties with their current value when calling save, this will re-validate them with your current method. You could also store the validation errors and re-validate only these properties (at least a bit cleaner)

A very simplified version of the approach would be:

this.GetType().GetProperties().ToList()
    .ForEach(prop => prop.SetValue(this, prop.GetValue(this, null), null));

Example for IDataErrorInfo

Upvotes: 4

Amol Bavannavar
Amol Bavannavar

Reputation: 2062

Implement IDataErrorInfo on entity & at saving time do following logic for checking error existence.

 foreach (var item in DataContext.GetChangeSet().Inserts)
 {
    IDataErrorInfo errofInfo = item as IDataErrorInfo;
     if(errofInfo != null && !string.IsNullOrEmpty(errofInfo.Error))
     {
         //// Error Exist;
     }
 }

 foreach (var item in DataContext.GetChangeSet().Updates)
 {
    IDataErrorInfo errofInfo = item as IDataErrorInfo;
     if(errofInfo != null && !string.IsNullOrEmpty(errofInfo.Error))
     {
         //// Error Exist;
     }
 }

This will improve error checking performance also.

Upvotes: 0

Related Questions