コード例 #1
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            _languageLocalization = (ILanguageLocalization)validationContext
                                    .GetService(typeof(ILanguageLocalization));

            string valueAsString = value as string;

            bool isValid = true;

            if (string.IsNullOrWhiteSpace(valueAsString))
            {
                isValid = false;
            }

            if (isValid && valueAsString.Length <= 0)
            {
                isValid = false;
            }

            if (isValid)
            {
                return(ValidationResult.Success);
            }

            return(new ValidationResult(
                       FormatErrorMessage(validationContext.DisplayName)));
        }
コード例 #2
0
ファイル: Context.cs プロジェクト: dotnetvn-projects/EzTask
 public static void Configure(this IApplicationBuilder applicationBuilder,
                              IHttpContextAccessor httpContextAccessor, IWebEnvironment webHostEnvironment,
                              SessionManager sessionManager, CookiesManager cookiesManager,
                              ILanguageLocalization languageLocalization, IAccountContext accountContext)
 {
     _httpContextAccessor  = httpContextAccessor;
     CurrentAccount        = accountContext;
     _cookiesManager       = cookiesManager;
     _sessionManager       = sessionManager;
     _webHostEnvironment   = webHostEnvironment;
     _languageLocalization = languageLocalization;
 }
コード例 #3
0
        public void AddValidation(ClientModelValidationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            _languageLocalization = context.ActionContext.
                                    HttpContext.RequestServices.InvokeComponents <ILanguageLocalization>();

            ValidatorUtils.MergeAttribute(context.Attributes, "data-val", "true");
            ValidatorUtils.MergeAttribute(context.Attributes, "data-val-emailfield", FormatErrorMessage(context.ModelMetadata.GetDisplayName()));
        }
コード例 #4
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            _languageLocalization = (ILanguageLocalization)validationContext
                                    .GetService(typeof(ILanguageLocalization));

            if (value == null)
            {
                return(ValidationResult.Success);
            }

            string valueAsString = value as string;

            // Use RegEx implementation if it has been created, otherwise use a non RegEx version.
            if (_regex != null)
            {
                var isMatch = valueAsString != null && _regex.Match(valueAsString).Length > 0;
                if (isMatch)
                {
                    return(ValidationResult.Success);
                }
                return(new ValidationResult(
                           FormatErrorMessage(validationContext.DisplayName)));
            }
            else
            {
                int atCount = 0;

                foreach (char c in valueAsString)
                {
                    if (c == '@')
                    {
                        atCount++;
                    }
                }

                var isMatch = (valueAsString != null &&
                               atCount == 1 &&
                               valueAsString[0] != '@' &&
                               valueAsString[valueAsString.Length - 1] != '@');

                if (isMatch)
                {
                    return(ValidationResult.Success);
                }
                return(new ValidationResult(
                           FormatErrorMessage(validationContext.DisplayName)));
            }
        }
コード例 #5
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            _languageLocalization = (ILanguageLocalization)validationContext
                                    .GetService(typeof(ILanguageLocalization));

            int valueAsInt;

            int.TryParse(value as string, out valueAsInt);

            bool isValid = true;

            if (valueAsInt < _minimum || valueAsInt > _maximum)
            {
                isValid = false;
            }

            if (isValid)
            {
                return(ValidationResult.Success);
            }

            return(new ValidationResult(
                       FormatErrorMessage(validationContext.DisplayName)));
        }