验证语言设置

Validation language settings

提问人:Aizolex 提问时间:10/9/2023 最后编辑:Peter MortensenAizolex 更新时间:10/9/2023 访问量:25

问:

考虑:

public class FilePathAttribute : ValidationAttribute
{
    private readonly string[] _extensions;

        public FilePathAttribute(string[] extensions)
        {
            _extensions = extensions;
        }

        protected override ValidationResult IsValid(object? value, ValidationContext validationContext)
        {
            var file = value as IFormFile;
            if(file != null)
            {
                var extension = Path.GetExtension(file.FileName);
                if(!_extensions.Contains(extension.ToLower()))
                {
                    return new ValidationResult($"Bu dosya uzantısı kabul edilemiyor. Dosya yalnızca '.png','.jgp','.jpeg','.txt','.pdf', '.docx' uzantılarını destekler.");
                }
            }
            return ValidationResult.Success;
        }
    }


       public class SpendingAmountAttribute : ValidationAttribute
        {
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                if (value is decimal spendingAmount && spendingAmount > 0)
                {
                    return ValidationResult.Success;
                }

                return new ValidationResult("Harcama tutarı 0'dan büyük olmalıdır.");
            }

        }

     public class ExpenditureCreateVM
     {
         public int Id { get; set; }

         [Required(ErrorMessage = "Harcama miktarı zorunludur.")]
         [SpendingAmount(ErrorMessage = "Harcama miktarı 0'dan büyük olmalıdır.")]
         public decimal SpendingAmount { get; set; }

         public DateTime RequestDateOfLeave = DateTime.Now;

         public DateTime? DateOfExpenseApproval { get; set; }

         public ExpenditureType Type { get; set; }

         public CurrencyType CurrencyType { get; set; }

         public ExpenditureStatus Status = ExpenditureStatus.OnayBekliyor;


         [Required(ErrorMessage = "Lütfen bir dosya ekleyiniz.")]
         [FilePath(new string[] { ".jpg", ".png", "jpeg", ".png", ".docx", ".txt" })]
         public string? FilePath { get; set; }

         public IFormFile File { get; set; }

         public int UserId { get; set; }

         public string? CompanyName { get; set; }
     }
    <form asp-area="Employee" asp-controller="Expenditure" asp-action="Create" id="formAccountSettings" method="post" enctype="multipart/form-data">
        <div class="row">
            <div class="form-group">
                <label asp-for="File">Dosya Seç:</label>
                <input type="file" asp-for="File" name="File" id="dosya" class="form-control-file" />
                <span asp-validation-for="File" class="text-danger"></span>

            </div>
            <div class="mb-3 col-md-6">
                <label class="form-label"> Harcama Türü </label>
                <select asp-for="Type" class="form-select" id="expenditureType" required>
                    <option value="0"> Seyehat Harcamaları </option>
                    <option value="1"> Yeme İçme Harcamaları </option>
                    <option value="2"> Ofis Malzemeleri ve Ekipmanları Harcamaları </option>
                    <option value="3"> Taşıt Giderleri </option>
                    <option value="4"> Toplantı ve Etkinlik Harcamaları </option>
                    <option value="5"> İş Telekomünikasyon Harcamaları </option>
                    <option value="5"> İş İle İlgili Diğer Harcamalar </option>
                </select>
                    <span asp-validation-for="Type" class="text-danger"></span>

            </div>

            <div class="mb-3 col-md-6">
                <label asp-for="@Model.SpendingAmount" class="form-label"> Harcama Tutarı </label>
                <input class="form-control" type="text" id="spendingAmount" asp-for="@Model.SpendingAmount" name="spendingAmount" placeholder="000" />
                <span asp-validation-for="SpendingAmount" class="text-danger"></span>

我按如下方式设置验证设置。当我单击“创建”按钮而不执行任何操作时,文件部分中没有任何错误消息。费用金额字段的“不能为空”消息以英文显示。我希望它是土耳其语的。

此外,当我在费用金额字段中输入负值时,它只是重新加载页面而不显示任何错误消息。我该如何解决这个问题?

当我使用 FluentValidation 时,我执行的负数验证以土耳其语显示。但是,我想创建更全面的验证,因此我想使用自定义验证。

asp.net-mvc asp.net-core 属性 data-annotations customvalidator

评论

0赞 Rena 10/10/2023
嗨,@Aizolex,对于您的共享代码,我还没有看到任何带有消息的验证,您在哪里定义它?cannot be empty'

答: 暂无答案