Question

La lecture du code source Oxite, j'ai trouvé que validateurs sauver mauvais nom de la propriété avec des 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."));

Quel est le but dont suffixes? Comment les utiliser?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top