public static ValidationRules AccountRoleCode(ValidationRules rules, object viewModel, object dataContext)
        {
            return rules
                .AddRule("Account Role is required.", delegate(object value)
                {
                   return (value!=null) && ((OptionSetValue)value).Value!=null;

                });

        }
        public static ValidationRules BirthDate(ValidationRules rules, object viewModel, object dataContext)
        {
            Contact self = (Contact)viewModel;
            return rules
                    .AddRule("Birthdate can't be in the future", delegate(object value)
                    {
                        DateTime birthdate =(DateTime)value;
                        return (birthdate < DateTime.Today);

                    });

        }
Beispiel #3
0
        public static ValidationRules PublishDate(ValidationRules rules, object viewModel, object dataContext)
        {
        
            Book self = (Book)viewModel;
            return rules
                    .AddRule("Publish date can't be more than 1 year in the future", delegate(object value)
                    {
                        DateTime publishDate = (DateTime)value;
                        return (publishDate < DateTimeEx.DateAdd(DateInterval.Days, 365,DateTime.Today));

                    });

        }
        public static ValidationRules ProductId(ValidationRules rules, object viewModel, object dataContext)
        {

            QuoteDetail self = Knockout.UnwrapObservable<QuoteDetail>(viewModel);
            return rules
                   .AddRule("Select either a product or provide a product description.", delegate(object value)
                   {
                       // Only a productdescription or productid can be selected
                       string productDescription = Knockout.UnwrapObservable<string>(self.ProductDescription);
                       bool isValid = String.IsNullOrEmpty(productDescription) || (value == null);

                       return isValid;
                   }
               );

        }
        public static ValidationRules WriteInProduct(ValidationRules rules, object viewModel, object dataContext)
        {
            
            QuoteDetail self = Knockout.UnwrapObservable<QuoteDetail>(viewModel);
            return rules
                   .AddRule("Select either a product or provide a product description.", delegate(object value)
                   {
                       // Only a productdescription or productid can be selected
                       EntityReference productid = Knockout.UnwrapObservable<EntityReference>(self.ProductId);
                       bool isValid = String.IsNullOrEmpty((string)value) || (productid == null);                

                       return isValid;
                   }
               );

        }
        public static ValidationRules FirstName(ValidationRules rules, object viewModel,object dataContext)
        {

            Contact self = Knockout.UnwrapObservable<Contact>(viewModel);
            return rules
                    .AddRequiredMsg("Enter a first name")
                   .AddRule("Must be less than 200 chars", delegate(object value)
                   {                
                        string valueText = (string)value;
                        return (valueText.Length < 200);
                   })
                   .AddRule("Firstname can't be the same as the lastname", delegate(object value)
                    {

                        bool isValid = true;
                        string lastName = Knockout.UnwrapObservable<string>(self.LastName);
                        if (lastName != null && (string)value == lastName)
                            isValid = false;

                        return isValid;
                    }
               );

        }
        public static ValidationRules ValidateRecord1RoleId(ValidationRules rules, object viewModel, object dataContext)
        {
            return rules
               .AddRule(ResourceStrings.RequiredMessage, delegate(object value)
               {
                   return (value != null) && ((EntityReference)value).Id != null;

               });



        }
Beispiel #8
0
        public static GridValidatorDelegate ConvertToGridValidation(ValidationRuleDelegate ruleDelegate)
        {
            GridValidatorDelegate validationFunction =  delegate(object value, object item)
                {
                    ValidationRules rules = new ValidationRules();
                    rules = ruleDelegate(rules,item, null);
                    ValidationResult result = new ValidationResult();
                    result.Valid = true;
                    Dictionary validationRules = Dictionary.GetDictionary(rules);
                    foreach (string key in validationRules.Keys)
                    {
                        // Find the ko validation rule
                        if (ValidationApi.Rules.ContainsKey(key))
                        {
                            ValidationRule targetRule = (ValidationRule)ValidationApi.Rules[key];
                            ValidationRule sourceRule = (ValidationRule)validationRules[key];
                            result.Valid = targetRule.Validator(value, sourceRule.Params==null ? targetRule.Params : sourceRule.Params);
                            result.Message = String.IsNullOrEmpty(targetRule.Message) ? sourceRule.Message : targetRule.Message;
                        }
                        else if (key == "validation")
                        {
                            // Anon rule - can be either a single or array - we assume it's an array created by the ValidationRules class
                            List<AnonymousRule> anonRules = (List<AnonymousRule>)validationRules[key];
                            foreach (AnonymousRule rule in anonRules)
                            {
                                result.Valid = rule.validator(value);
                                result.Message = rule.message;
                                if (!result.Valid)
                                    break;
                            }

                        }
                        if (!result.Valid)
                            break;
                    }

                    return result;
                };

            return validationFunction;
        }
 private static ValidationRules ValidateRecord1RoleId(ValidationRules rules, object viewModel, object dataContext)
 {
     return rules
         .AddRule("Required", delegate(object value) {
             return (value != null) && ((EntityReference)value).Id != null;
         });
 }