后缀 .RequiredError、.FormatError 等的用途是什么

发布于 2024-07-23 09:43:57 字数 534 浏览 6 评论 0原文

阅读 Oxite 源代码,我发现验证器会保存带有一些后缀的错误属性名称(RequiredError、MaxLengthExceededError、InvalidError、FormatError),

validationState.Errors.Add(CreateValidationError(user.Name, "Name.RequiredError", "Name is not set"));

validationState.Errors.Add(CreateValidationError(user.Name, "Name.MaxLengthExceededError", "Username must be less than or equal to {0} characters long.", 256));

validationState.Errors.Add(CreateValidationError(user.Email, "Email.InvalidError", "Email is invalid."));

这些后缀的目的是什么? 他们怎么用的?

Reading the Oxite source code, I have found that validators save bad property name with some suffixes (RequiredError, MaxLengthExceededError, InvalidError, FormatError)

validationState.Errors.Add(CreateValidationError(user.Name, "Name.RequiredError", "Name is not set"));

validationState.Errors.Add(CreateValidationError(user.Name, "Name.MaxLengthExceededError", "Username must be less than or equal to {0} characters long.", 256));

validationState.Errors.Add(CreateValidationError(user.Email, "Email.InvalidError", "Email is invalid."));

What is the purpose of whose suffixes? How they used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写下不归期 2024-07-30 09:43:57

我的猜测是,它们是恒定的、机器友好的值,可用于唯一地标识错误,并可用于为全球化站点获取本地化资源。


我很擅长猜测:不过我很

    protected ValidationError CreateValidationError(
        object value, string validationKey, string validationMessage, 
        params object[] validationMessageParameters)
    {
        if (validationMessageParameters != null && 
            validationMessageParameters.Length > 0)
        {
            validationMessage = string.Format(
              validationMessage, validationMessageParameters);
        }

        return new ValidationError(
            validationKey,
            value,
            new InvalidOperationException(
              localize(validationKey, validationMessage))
            );
    }

    private string localize(string key, string defaultValue)
    {
        if (phrases == null)
            phrases = localizationService.GetTranslations();

        Phrase foundPhrase = phrases
          .Where(p => p.Key == key && p.Language == site.LanguageDefault)
          .FirstOrDefault();

        if (foundPhrase != null)
            return foundPhrase.Value;

        return defaultValue;
    }

好奇。 因为异常通常不应该被本地化。

My guess is that they're constant, machine-friendly values that can be used to uniquely identify the error and can be used to fetch localized resources for your globalized site.


I'm a good guesser:

    protected ValidationError CreateValidationError(
        object value, string validationKey, string validationMessage, 
        params object[] validationMessageParameters)
    {
        if (validationMessageParameters != null && 
            validationMessageParameters.Length > 0)
        {
            validationMessage = string.Format(
              validationMessage, validationMessageParameters);
        }

        return new ValidationError(
            validationKey,
            value,
            new InvalidOperationException(
              localize(validationKey, validationMessage))
            );
    }

    private string localize(string key, string defaultValue)
    {
        if (phrases == null)
            phrases = localizationService.GetTranslations();

        Phrase foundPhrase = phrases
          .Where(p => p.Key == key && p.Language == site.LanguageDefault)
          .FirstOrDefault();

        if (foundPhrase != null)
            return foundPhrase.Value;

        return defaultValue;
    }

Curious, though. Since exceptions generally shouldn't be localized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文