Giter VIP home page Giter VIP logo

aspnetcore.customvalidation's Introduction

ASP.NET Core Custom Validation

This is a custom model validation library for ASP.NET Core projects.

Whats new in version 2.0.0?

  1. This release added localization support for error messages.
  2. FileTypeAttribute, FileMaxSizeAttribute and FileMinSizeAttribute are newly added ValidationAttributes in this release.
  3. TinyMceRequiredAttribute has been renamed to TextEditorRequiredAttribute.
  4. This release also includes some imporant bug fixes.

How do I get started?

Configuring TanvirArjel.CustomValidation into your ASP.NET Core project is simple as below:

First install the lastest version of AspNetCore.CustomValidation nuget package into your project as follows:

Install-Package AspNetCore.CustomValidation

Then decorate your class properties with appropriate custom validation attributes as follows:

pulic class Employee
{
    [Display(Name = "First Number")]
    public int FirstNumber { get; set; }

    [CompareTo(nameof(FirstNumber), ComparisonType.GreaterThanOrEqual)]
    [Display(Name = "Second Number")]
    public int? SecondNumber { get; set; }
    
    [File(FileType.Jpg, MaxSize = 1024)]
    public IFormFile Photo { get; set; }
}

Client Side validation:

To enable client client side validation for ASP.NET Core MVC or Razor Pages:

  1. First in the ConfirugeServices method of the Startup class:
public static void ConfigureServices(IServiceCollection services)
{
    services.AddAspNetCoreCustomValidation();
}
  1. Then please add the latest version of aspnetcore-custom-validation.min.js file as follows:
@section Scripts {
  @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
  <script type="text/javascript" src="~/lib/aspnetcore-custom-validation/aspnetcore-custom-validation.min.js"</script>
}

Or

<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript" src="~/lib/aspnetcore-custom-validation/aspnetcore-custom-validation.min.js"</script>

You can download the aspnetcore-custom-validation.min.js from here aspnetcore-custom-validation-npm

Or using Visusl Studio Libman as follows:

1 ) wwwroot > lib> Add > Client Side Libray

2. Provider: jsdelivr
   Libray: aspnetcore-custom-validation
3. Click install

What contains in version 2.0.0?

In version 2.0.0 AspNetCore.CustomValidation contains the following validation attributes:

1. FileAttribute To validate file type, file max size, file min size etc.

2. FileTypeAttribute To validate type of a file.

3. FileMaxSizeAttribute To validate allowed max size of a file.

4. FileMinSizeAttribute To validate allowed min size of a file.

5. MaxAgeAttribute To validate maximum age against date of birth value of DateTime type.

6. MinAgeAttribute To validate minimum required age against a date of birth value of DateTime type.

7. MaxDateAttribute To set max value validation for a DateTime field.

8. MinDateAttribute To set min value validation for a DateTime field.

9. TextEditorRequiredAttribute To enforce required valiaton attribute on the online text editors like TinyMCE, CkEditor etc.

10. CompareToAttribute To compare one property value against another property value of the same object. Comparison types are: Equal, NotEqual, GreaterThan, GreatherThanOrEqual, SmallerThan, SmallerThanOrEqual

Dynamic Validation

From version 1.4.0, validation against dynamic values from database, configuration file or any external source added for the following type: 1. File Type: with ValidateFile() method 1. DateTime Type: with ValidateMaxAge() and ValidateMinAge() method as follows:

public class Employee : IValidatableObject
{
    public DateTime? DateOfBirth { get; set; }
    public IFormFile Photo { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        List<ValidationResult> validationResults = new List<ValidationResult>();
        FileOptions fileOptions = new FileOptions()
        {
            FileTypes = new FileType[] {FileType.Jpeg,FileType.Jpg},
            MinSize = 124,
            MaxSize = Convert.ToInt32(AppSettings.GetValue("DemoSettings:MaxFileSize"))
        };

        ValidationResult minAgeValidationResult = validationContext.ValidateMinAge(nameof(DateOfBirth), 10, 0, 0);
        validationResults.Add(minAgeValidationResult);
        
        ValidationResult fileValidationResult = validationContext.ValidateFile(nameof(Photo), fileOptions);
        validationResults.Add(fileValidationResult);
        return validationResults;
    }
}

Note

Dont forget to request your desired validation attribute by submitting an issue.

Request

If you find this library useful, please don't forget to encouraging me to do such more stuffs by giving a star to this repository. Thank you.

aspnetcore.customvalidation's People

Contributors

exluzzzivo avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.