In this session of ASP.NET 5.0 / Core Tutorial, we will talk about Range Attribute, Custom Attribute and Custom Validation in detail. In continuation of Session 38, we are going to introduce one more validation attribute which is Range attribute. Also, we will show you the way that you can customize the attributes and the way that you can create a custom validation attribute.
Range Attribute
This attribute can set a range of valid value for each property with different property type. Also, Range attribute is in System.ComponentModel.DataAnnotations namespace, thus, for using this attribute, we need to add this namespace to the Model or the ViewModel. Moreover, it has several version of overload that can be used based on our requirement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class CreateCostViewModel { [Range(0.0,100000.0)] public decimal Amount { get; set; } [Range(typeof(DateTime),"01/01/1996","01/01/2050")] public DateTime Date { get; set; } public string Comment { get; set; } public string Category { get; set; } public List<SelectListItem> Categories { get; set; } public List<string> SelectedCategories { get; set; } public int PaymentMethod { set; get; } public List<PaymentMethod> PaymentMethods { get; set; } public bool MakeFormClear { get; set; } } |
As you can see in above code, we decorate Amount and Date properties with Range Attribute.
Custom Attribute in ASP.NET 5.0 / Core
For this purpose, we need to create a class which should be inherited form the attribute that we want to customize. We can create the class everywhere in our project, but to keep the project structure clear, we recommend creating a specific folder for this purpose. Also, for as we need to use Attribute classes, we should add their namespace (System.ComponentModel.DataAnnotations) to the class.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System.ComponentModel.DataAnnotations; namespace DailyCostWebApplication.CustomHtmlHelpers { public class AllowedCharcterLength : StringLengthAttribute { } } |
Then we can do the required customization with the Base method.
1 2 3 4 5 6 7 8 9 10 |
public class AllowedCharcterLength : StringLengthAttribute { public AllowedCharcterLength() : base(10) { } } |
For example, we customized StringLengthAttribute and created AllowedCharcterLength attribute. AllowedCharcterLength attribute does not need any input parameter and we can decorate string properties with this attribute. the decorated property with this attribute, would have maximum length of 10 characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System.ComponentModel.DataAnnotations; namespace DailyCostWebApplication.CustomHtmlHelpers { public class AllowedCharcterLength : StringLengthAttribute { public AllowedCharcterLength() : base(10) { } } } |
Create Custom Validation in ASP.NET 5.0 / Core
For creating custom validation, again we need to create a class and the class should be inherited form ValidationAttribute class. Also, again we need to add System.ComponentModel.DataAnnotations namespace to the created class.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.ComponentModel.DataAnnotations; namespace DailyCostWebApplication.CustomHtmlHelpers { public class AllowedCharacterRange : ValidationAttribute { } } |
Then, we should override IsValid method and set the required conditions. Later, based on the value and the condition, method return a Boolean result.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class AllowedCharacterRange : ValidationAttribute { public override bool IsValid(object value) { string str = Convert.ToString(value); return str.Length >= 5 && str.Length <= 10; } } |
For example, AllowedCharacterRange attribute checks the string length and return true if the string is between 5 to 10 characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.ComponentModel.DataAnnotations; namespace DailyCostWebApplication.CustomHtmlHelpers { public class AllowedCharacterRange : ValidationAttribute { public override bool IsValid(object value) { string str = Convert.ToString(value); return str.Length >= 5 && str.Length <= 10; } } } |
If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram or YouTube. Also you can have access to list of all sessions HERE.
You can download this Session Slides form HERE