Consider:
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>
I set the validation settings as follows. When I click the Create button without doing anything, there isn't any error message in the file section. The 'cannot be empty' message for the expense amount field is coming in English. I want it to be in Turkish.
Additionally, when I enter a negative value in the expense amount field, it just reloads the page without showing any error message. How can I fix this?
When I use FluentValidation, the negative number validation I perform appears in Turkish. However, I want to create a more comprehensive validation, so I want to use Custom Validation.