Skip to content

Remote validation is not working #22549

@evertongmdr

Description

@evertongmdr

Describe the bug

Remote validation is not working, does not call method on the controller

To Reproduce

Steps to reproduce the behavior:

Using the version of ASP.NET Core '3.1'

  • Create a user controller and inside it has a method named EmailExists
  • I use EmailExist method inside my User entity to check if the email exists

Startup

    {
 
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
             services.AddControllers().
                AddNewtonsoftJson(options =>
                options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

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

            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
            services.AddScoped<IUserRepository, UserRepository>();
        }

        // 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.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

           
        }
    }

### Controller
namespace BuyProductAPI.Controllers
{
    [ApiController]
    [Route("api/users")]
    public class UsersController: ControllerBase
    {
        private readonly IUserRepository _userRepository;
        public UsersController(IUserRepository userRepository)
        {
            _userRepository = userRepository;
            
        }

        [AcceptVerbs("Get","Post")]
        [Route("email-exists")]
        public async Task<IActionResult> EmailExists(string email)
        {
            var emailExists = await _userRepository.EmailExists(email);

            if (emailExists)
                return Ok(false);

            return Ok(true);
        }

    }
}

###Entity

namespace BuyProductAPI.Entites
{
    public class User
    {
        [Key]
        public Guid Id { get; set; }

        [Required(ErrorMessage = "O nome é obrigatório")]
        [MaxLength(50, ErrorMessage = "O nome deve conter entre 3 a 50 caracteres")]
        [MinLength(3, ErrorMessage = "o nome deve conter entre 3 a 50 caracteres")]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "O sobrenome é obrigatório")]
        [MaxLength(50, ErrorMessage = "O sobrenome deve conter entre 3 a 50 caracteres")]
        [MinLength(3, ErrorMessage = "O sobrenome deve conter entre 3 a 50 caracteres")]
        public string LastName { get; set; }
        
        [Required(ErrorMessage ="O e-mail é obrigatório")]
        [EmailAddress(ErrorMessage ="E-mail inváido")]
        [Remote(action: "EmailExists", controller: "Users", ErrorMessage ="Email already exists")]
        public string Email { get; set; } // usar unique próxima vez ;)

        [Required(ErrorMessage ="A senha é obrigatória")]
        public string Password { get; set; }
        public virtual ICollection<Order> Orders { get; set; }

    }
}

Expected behavior

The above view should work and show the validation error.

Metadata

Metadata

Assignees

Labels

Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.Status: No Recent Activityaffected-very-fewThis issue impacts very few customersbugThis issue describes a behavior which is not expected - a bug.feature-model-bindinginvestigateold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labelsseverity-minorThis label is used by an internal tool

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions