Пример #1
0
        public ComplexValidatorDataProvider <T, TData> For <TData>(string fieldName, IObservable <TData> observable,
                                                                   Func <IValidator <T>, TData> immediateGetter)
        {
            this.CheckNotSealed();
            if (this.Fields.ContainsKey(fieldName))
            {
                throw new InvalidOperationException(
                          "There are already rules defined for this validation field. " +
                          "Combine the mutliple For chains into one or change each one to correspond to a unique name.");
            }

            var complexValidatorDataProvider = new ComplexValidatorDataProvider <T, TData>(this, fieldName, observable,
                                                                                           immediateGetter, this._forcedRefreshSignals);

            this.Fields.Add(fieldName, complexValidatorDataProvider.Molecule);
            return(complexValidatorDataProvider);
        }
Пример #2
0
 public ComplexValidatorMolecule(ComplexValidatorDataProvider <T, TData> complexValidatorDataProvider)
 {
     this._complexValidatorDataProvider = complexValidatorDataProvider;
     this._refCountedObservable         = this._complexValidatorDataProvider
                                          .OnEachExecuteCancellingPrevious(async x =>
     {
         try
         {
             using (this._complexValidatorDataProvider.Validator.ObservableEntranceExitCounter.Enter())
             {
                 var validationResults = new List <IValidationResult>();
                 foreach (var validationRule in this.Rules)
                 {
                     var validationResult = await validationRule.ValidationFunction.Invoke(x,
                                                                                           this._complexValidatorDataProvider.Validator);
                     if (validationResult.ValidationResultType == ValidationResultType.Valid)
                     {
                         continue;
                     }
                     validationResults.Add(validationResult);
                 }
                 return(new ValidationFieldResults(this._complexValidatorDataProvider.FieldName,
                                                   validationResults));
             }
         }
         catch (Exception e)
         {
             this.Log().Error($"Error while validating field {complexValidatorDataProvider.FieldName}:", e);
             return(new ValidationFieldResults(this._complexValidatorDataProvider.FieldName,
                                               new List <IValidationResult>
             {
                 new ValidationResult
                 {
                     Message = Localisation.ErrorWhileValidating,
                     ValidationResultType = ValidationResultType.Error
                 }
             }));
         }
     })
                                          .Catch <ValidationFieldResults, ObjectDisposedException>(
         ex => Observable.Empty <ValidationFieldResults>())
                                          .Publish(new ValidationFieldResults(complexValidatorDataProvider.FieldName,
                                                                              new List <IValidationResult>()))
                                          .RefCount();
 }