Pergunta

A leitura do código fonte Oxite, eu descobri que validadores Salvar mau nome da propriedade com alguns sufixos (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."));

Qual é o propósito de cujos sufixos? Como eles usaram?

Foi útil?

Solução

Meu palpite é que eles são constantes, valores-friendly máquina que podem ser usados ??para identificar exclusivamente o erro e pode ser usado para buscar recursos localizados para o seu site globalizado.


Eu sou um bom adivinho:

    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;
    }

curioso, no entanto. Desde exceções geralmente não deve ser localizada.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top