Skip to content

Small library to extend validation attributes on blazor forms

License

Notifications You must be signed in to change notification settings

nicolaidanekov/ValidationExtensions

 
 

Repository files navigation

ValidationExtensions

.NET nuget

The motivation behind this small project is simple. Just imagine you have the following model in Blazor:

public class MyModel
{
    [Required]
    public string Title { get; set; }

    [Required]
    public string Content { get; set; }

    [Required]
    public bool IsPublished { get; set; }
}

Now as a consumer you have to provide all of those 3 values. That is all good and nice, but what if we want to say: "Okay as long as it doesn't get published, we don't have to provide the content?". Well that does not work with the default implementation.

Here is where this small library comes into play:

public class MyModel
{
    [Required]
    public string Title { get; set; }

    [RequiredIf(nameof(IsPublished), true)]
    public string Content { get; set; }

    [Required]
    public bool IsPublished { get; set; }
}

Now Title will always be required. But as long as IsPublished is false Content can be null or empty.

Get Started

To install either go the nuget or execute the following command:

dotnet add LinkDotNet.ValidationExtensions

Example

using LinkDotNet.ValidationExtensions;

public class BlogArticle
{
    [Required]
    public string Title { get; set; }

    [RequiredIf(nameof(IsPublished), true)]
    public string ArticleContent { get; set; }

    [RequiredIfNot(nameof(ArticleContent), null)]
    public string ReplacementContent { get; set; }
    
    [Required]
    public bool? NoticeByEmail { get; set; }

    [RequiredDynamic(nameof(ValidateRequired_NoticeByEmail), "Notice by email is activated")]
    public string? EmailAddress { get; set; }
    
    [DynamicRange(typeof(decimal), minimum: 9.99, maximumPropertyName: nameof(MaximumPrice))]
    public decimal? MinimumPrice { get; set; }

    [DynamicRange(typeof(decimal), minimumPropertyName: nameof(MinimumPrice), maximum: 199.99)]
    public decimal? MaximumPrice { get; set; }

    [DynamicRange<double>(minimum: 0.1d, maximumPropertyName: nameof(MaximumWeight))]
    public double? MinimumWeight { get; set; }

    [DynamicRange<double>(minimumPropertyName: nameof(MinimumWeight), maximum: 500d)]
    public double? MaximumWeight { get; set; }

    private static bool ValidateRequired_NoticeByEmail(BlogArticle value)
    {
        if (!value.NoticeByEmail.HasValue)
        {
            return false;
        }

        if (!value.NoticeByEmail.Value)
        {
            return false;
        }

        if (string.IsNullOrWhiteSpace(value.EmailAddress))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

Currently implemented additional attributes:

  • RequiredIf
  • MinLengthIf / MaxLengthIf
  • RangeIf
  • MinIf / MaxIf
  • Min / Max
  • Dynamic
  • DynamicRange
  • DynamicRange<T> C# 11 Generic Attributes
  • FutureDateValidation / PastDateValidation

About

Small library to extend validation attributes on blazor forms

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%