2 Answer(s)
- 
    0
Hi, it's the same as how to create it on the .NET side. You need to create an attribute that inherits from
ValidationAttributeand implement theIsValidmethod:[AttributeUsage(AttributeTargets.Property)] //only can be used in properties public class YourCustomAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { //implement } }Then use the attribute on top of your property or class:
public class CreateNewsDto { [Required(ErrorMessage = "The {0} field is required")] [StringLength(200, ErrorMessage = "The {0} must not exceed {1} characters")] [YourCustom] //your attribute public string TitleAr { get; set; } //... }Since this is not related to ABP, you can refer to https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/reflection-and-attributes/ for further info.
Regards.
 - 
    0
Which project can i implement YourCustomAttribute and also note that my Attribute will ensure that it is really image file not pdf and user just changed extension so i mean it is logic that's why i ask you which project will implement this attribute to put in mt dto which in contract project ?
Hi, yes you can create and define your attribute in the
*.Application.Contractsproject. You should basically create an attribute, inherit it from theValidationAttributebase class of .NET, then override theIsValidmethod and apply your own logic.Btw, in one of my old project, I had similar requirements and created an
AllowedExtensionsAttributeas follows:public class AllowedExtensionsAttribute : ValidationAttribute { private readonly string[] _extensions; public AllowedExtensionsAttribute(string[] extensions) { _extensions = extensions; } protected override ValidationResult IsValid( object value, ValidationContext validationContext) { if (value == null) { return ValidationResult.Success; } var file = value as IFormFile; var extension = Path.GetExtension(file.FileName); if (file.Length > 0 && file != null) { if (!_extensions.Contains(extension.ToLower())) { return new ValidationResult(GetErrorMessage()); } } return ValidationResult.Success; } public string GetErrorMessage() { return $"This extension is not allowed!"; } }and use the attribute:
[AllowedExtensions(new string[] { ".jpg", ".png", ".jpeg" })] public IFormFile CoverImageFile { get; set; }
Since this is not related to ABP, you can refer to https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/reflection-and-attributes/ for further info.