1

enter image description hereI have implemented a custom validation attribute to check column uniqueness. I want to check if the provided value already exists in the database or not. Here is my code:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
public class UniqueAttribute : ValidationAttribute
{
    public UniqueAttribute()
    {
    }

    public override bool RequiresValidationContext => true;

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        return ValidationResult.Success;
    }
    
}

the validationContext in IsValid method always returns null. How it can be fixed?

Startup.cs : ConfigureServices method

public void ConfigureServices(IServiceCollection services)
        {
            services.AddApiVersioning(o =>
            {
                o.ReportApiVersions = true;
                o.AssumeDefaultVersionWhenUnspecified = true;
                o.DefaultApiVersion = new ApiVersion(1, 0);
            });

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

            services.AddDatabaseDeveloperPageExceptionFilter();

            services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false)
                .AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>();

            services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

            services.AddAuthentication()
                .AddIdentityServerJwt();

            services.AddScoped<IClaimsTransformation, ClaimsTransformation>();

            RegisterRepository(services);
            RegisterServices(services);
            RegisterAutoMapper(services);

            services.AddControllersWithViews()
            .AddJsonOptions(opts => opts.JsonSerializerOptions.PropertyNamingPolicy = null);

            services.AddRazorPages();
            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseMigrationsEndPoint();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            if (!env.IsDevelopment())
            {
                app.UseSpaStaticFiles();
            }

            app.UseRouting();

            app.UseAuthentication();
            app.UseIdentityServer();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });

            app.UseSpa(spa =>
            {
                // To learn more about options for serving an Angular SPA from ASP.NET Core,
                // see https://go.microsoft.com/fwlink/?linkid=864501

                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });
        }

Model class :

public class Category
    {
        [Unique]
        public string Name { get; set; }
        public string Description { get; set; }
    }
  • "the validationContext in IsValid method always returns null" What do you mean return null ? You mean that you cannot see the value of the validationContext ? – Qing Guo Aug 30 '22 at 07:34
  • I had old code causing this same issue. I was using: `base.IsValid(value, validationContext)` and I had to change it to: `ValidationResult.Success` – Post Impatica Feb 07 '23 at 17:40

1 Answers1

0

Below is an example to check if the provided value already exists in the database or not, you can refer to it. UniqueAttribute:

 public class UniqueAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value,ValidationContext validationContext)
        {
            var context = (MvcMovieContext)validationContext.GetService(typeof(MvcMovieContext));//change the MvcMovieContext to your DbContext
            if (!context.Movie.Any(a => a.Company == value.ToString()))
            {
                return ValidationResult.Success;
            }
            return new ValidationResult("Company exists");
        }
}

Movie:

public class Movie
    {           
        [Unique]
        public string Company { get; set; }          
    }

Create view:

@model MvcMovie.Models.Movie

@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Movie</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>                
            <div class="form-group">
                <label asp-for="Company" class="control-label"></label>
                <input asp-for="Company" class="form-control" />
                <span asp-validation-for="Company" class="text-danger"></span>
            </div>               
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

result:

enter image description here

Qing Guo
  • 6,041
  • 1
  • 2
  • 10
  • Thanks for answering @Qing Guo. but my concern is about validationContext parameter that is already coming null. Also I have added snapshot for the same in Question – Mahesh Kantariya Aug 30 '22 at 08:21
  • @MaheshKantariya What is the version of asp.net core you have used? – Qing Guo Aug 30 '22 at 08:31
  • I am using .NET 5 version – Mahesh Kantariya Aug 30 '22 at 08:37
  • @MaheshKantariya Unfortunately, I cannot reproduce your null problem. Could you share some code about how you use the UniqueAttribute and the startup? Have you try to create a new asp.net core project just for creating a new UniqueAttribute to see the ValidationContext value? – Qing Guo Aug 30 '22 at 09:35