public async Task ValidatorCollectionTests() { var error10 = "Must equal 10"; var error20 = "Must equal 20"; var collection = new ValidatorCollection <int>() .Add(e => (e == 10, error10)) .Add(e => (e == 20, error20)); IValidatorCollection <int> target1 = collection; IValidatorCollection target2 = collection; Assert.NotNull(target1); var(_, test1_10) = await target1.ValidateInput(40); var(_, test1_20) = await target1.ValidateInput(10); Assert.Equal(error10, test1_10); Assert.Equal(error20, test1_20); Assert.NotNull(target2); var(_, test2_10) = await target2.ValidateRawInput(40); var(_, test2_20) = await target2.ValidateRawInput(10); Assert.Equal(error10, test2_10); Assert.Equal(error20, test2_20); }
/// <summary> /// Register a new collection with a name /// </summary> /// <param name="name">name to store collection</param> /// <param name="coll">collection to store</param> /// <param name="overwrite">set if should replace existing name</param> /// <returns>true, if saved</returns> public bool Register(string name, IValidatorCollection coll, bool overwrite = true) { if (_data.ContainsKey(name) && !overwrite) { return(false); } _data[name] = coll; return(true); }
/// <summary> /// Evaluates the condition. /// </summary> /// <returns></returns> protected override bool EvaluateIsValid() { if (_validatorCollection == null) { _validatorCollection = new PageValidatorCollection(Page); } ICollection <ModelPropertyValidator> validators = GetPropertyValidators(); if (validators.Count > 0) { Type modelType = GetModelType(); IValidatableObject model = Activator.CreateInstance(modelType) as IValidatableObject; if (model != null) { // set the model with the form values List <ValidationResult> results = new List <ValidationResult>(); foreach (ModelPropertyValidator propertyValidator in validators) { PropertyInfo property = modelType.GetProperty(propertyValidator.PropertyName); object propertyValue; try { propertyValue = GetModelPropertyValue(property.Name, propertyValidator.ControlToValidate); } catch (ValidationException ex) { results.Add(ex.ValidationResult); continue; } property.SetValue(model, propertyValue, null); } results.AddRange(model.Validate(new ValidationContext(model, null, null))); foreach (ValidationResult result in results) { _validatorCollection.Add(new ValidationError(result.ErrorMessage, ValidationGroup)); } return(!results.Any()); } } return(true); }
/// <summary> /// Register a new collection with a name /// </summary> /// <param name="key">Enum value to store</param> /// <param name="coll">collection to store</param> /// <param name="overwrite">set if should replace existing name</param> /// <typeparam name="T">Enum type</typeparam> /// <returns>true, if saved</returns> public bool Register <T>(T key, IValidatorCollection coll, bool overwrite = true) where T : Enum { return(Register(key.ToString(), coll, overwrite)); }
protected virtual void ConfigureValidation([NotNull] IValidatorCollection validators) { }
/// <summary> /// Show message, request for input and use the default validator store to validate result /// </summary> /// <param name="message"></param> /// <param name="defaultValue"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static Task <T> Ask <T>(string message, T defaultValue = default, IValidatorCollection <T>?validators = null, StringConverterProvider?provider = null) { return(InputText.Create <T>( message, defaultValue, validators, provider) .RequestInput()); }
/// <summary> /// Initializes a new instance of the <see cref="ModelPage"/> class. /// </summary> /// <param name="validatorCollection">The validator collection.</param> public ModelPage(IValidatorCollection validatorCollection) { _validatorCollection = validatorCollection ?? new PageValidatorCollection(this); }
public void SetValidator(IValidatorCollection validators) { Validators = new ValidatorCollection <T>() .AddRange(validators.Cast <IValidator <T> >()); }
/// <summary> /// Initializes a new instance of the <see cref="ModelValidator"/> class. /// </summary> /// <param name="validatorCollection">The validator collection.</param> public ModelValidator(IValidatorCollection validatorCollection) { _validatorCollection = validatorCollection; }