TanvirArjel/CustomValidation

Exception when using CompareTo

Closed this issue · 4 comments

When using the CompareTo attribute, I am getting the following error when the view tries to load.
It happens when it executes the input tag helper on the server.

FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
System.Text.ValueStringBuilder.AppendFormatHelper(IFormatProvider provider, string format, ParamsArray args)

I tested using another attribute (RequiredIf) to make sure I set everything up correctly and its working just fine.

I am using...

  • TanvirArjel.CustomValidation.AspNetCore 1.2.1
  • .NET 5 with Razor Pages

I can provide more details if its helpful.

@TCook7 Please add your usage code. For example: The property decorated with the attribute.

@TanvirArjel

Here is the model that has the attribute (I only included the 2 properties that matter).

using System.ComponentModel.DataAnnotations;
using TanvirArjel.CustomValidation.Attributes;

public class WayMagazineRangeDTO
{        
        [Required]
        [Range(1000, 9999)]
        [Display(Name = "Starting Year")]
        public int? StartingYear { get; set; }

        [Required]
        [Range(1000, 9999)]
        [CompareTo(nameof(StartingYear), ComparisonType.GreaterThanOrEqual)]
        [Display(Name = "Ending Year")]
        public int? EndingYear { get; set; }
}

Here is the HTML. The error happens when the server tries to execute the input tag helper line.

<div class="col-lg-2">
     <label asp-for="PublicationGroup.WayMagazineRange.EndingYear" class="form-label"></label>
     <input asp-for="PublicationGroup.WayMagazineRange.EndingYear" class="form-control" />
     <span asp-validation-for="PublicationGroup.WayMagazineRange.EndingYear" class="text-danger"></span>
</div>

Here the line I added in my Startup.cs class

public void ConfigureServices(IServiceCollection services)
{
     services.AddAspNetCoreCustomValidation();

     ...
}

@TCook7 I could not reproduce this issue. I have found everything is working fine.

Here is my model properties:

[Required]
[Range(1000, 9999)]
[Display(Name = "Starting Year")]
public int? StartingYear { get; set; }

[Required]
[Range(1000, 9999)]
[CompareTo(nameof(StartingYear), ComparisonType.GreaterThanOrEqual, ErrorMessage = "The {0} should be greater than or equal {1}.")]
[Display(Name = "Ending Year")]
public int? EndingYear { get; set; }

Here is the validation message:

image

Maybe the issue lies in your error message formatting.

I'm having the same issue here. Was anyone ever able to find the cause?