DataForm中的Silverlight MVVM验证

时间:2009-11-13 02:06:44

标签: silverlight mvvm

我正在使用通用数据类,所以我不能使用ria服务属性来控制我的验证 - 所以我正在寻找一种方法来手动设置验证以在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;

所以在ASP MVC中,我只是在提交表单时手动检查这些规则...我想这就是我想在MVVM中做的事情(我只是不确定最好的方法) 。

ASP代码

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;
        }

2 个答案:

答案 0 :(得分:0)

Silverlight 3内置验证基于异常。

只是在你的通用Setter中抛出一个有意义的异常,你应该没问题。 记得在{Binding}上设置ValidatesOnException = True和NotifyOnValidationError = True。

Jesse在他的博客上有a good sample个例外验证。

答案 1 :(得分:0)

您可以使用MetadataTypeAttribute属性附加验证属性 一旦它们在DomainService中公开,RIA Services将自动在客户端上生成这些验证。

示例:

[MetadataType(typeof(ContactMd))]
public partial class Contact
{
    internal class ContactMd
    {
        [MyCustomValidation]
        public string Name { get; set; }
    }
}

MyCustomValidation是指从ValidationAttribute继承的任何内容。