views:

443

answers:

1

I am using generic data classes, so I can't use ria services attributes to control my validation - so I am looking for a way to manualy set up validation to work in a DataForm.

public partial class DataValue 
    {
        private Dictionary<string, string> _errors = new Dictionary<string, string>();

        public Dictionary<string, string> Errors
        {
            get { return _errors; }
        }

        public Object Value
        {  
            get 
            {
                object result = new object();
                switch ((DataType)this.ModelEntity.ModelItem.DataType)
                {
                    case DataType.Money: 
                        return result = this.ValueText.ParseNullableFloat(); 
                    case DataType.Integer: 
                        return result = this.ValueText.ParseNullableInt();
                    case DataType.Date:                        
                    case DataType.Time:
                        return result = this.ValueText.ParseNullableDateTime();
                    case DataType.CheckBox:
                        return result = this.ValueText;
                    default:
                        return result = this.ValueText;
                }                
            }

            set
            {
                if (!String.IsNullOrEmpty(value.ToString()))
                {
                    bool invalid = false;
                    switch ((DataType)this.ModelEntity.ModelItem.DataType)
                    {
                        case DataType.Money:
                            float val;
                            if (!float.TryParse(value.ToString(), out val)) invalid = true;
                            break;
                        case DataType.Integer:
                            int val2;
                            if (!Int32.TryParse(value.ToString(), out val2)) invalid = true;
                            break;
                        case DataType.Date:
                        case DataType.Time:
                            DateTime val3;
                            if (!DateTime.TryParse(value.ToString(), out val3)) invalid = true;
                            break;
                    }
                    if (invalid == false)
                        ValueText = value.ToString();
                    else
                    {
                        ValueText = "";
                        _errors.Add(this.ModelEntity.LocalName, "error writing " + value.ToString() + " to " + this.ModelEntity.ModelItem.Label);
                    }
                }
                else
                    ValueText = "";

            }
        }

public partial class ModelValidater : INotifyPropertyChanging, INotifyPropertyChanged {

 private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

 private int _ModelValidatorId;

 private int _ModelEntityId;

 private int _ValidatorType;

 private string _ValidatorParameters;

So in ASP MVC, I simply manually checked against these rules when the form was submitted... which I guess is pretty much what I want to do in MVVM (I am just not sure the best way to go about this).

ASP Code

protected bool ModelErrors(RecordDictionary record)
        {
            bool result = false;
            foreach (var field in record)
            {
                foreach (var error in field.Value.Errors)
                {
                    result = true;
                    ModelState.AddModelError(error.Key + "Validation", error.Value.ToString());
                }
            }
            return result;
        }
A: 

Silverlight 3 built-in validation is based on exceptions.

Just throw a meaningful exception in your generic Setter and you should be fine. Remember to set ValidatesOnException=True and NotifyOnValidationError=True on your {Binding}.

Jesse has a good sample of validation with exceptions on his blog.

JustinAngel