Base solution for your next web application
Open Closed

Custom AddValidationErrors not Reached #931


User avatar
0
florinciubuc created

Hello. I have a question: Even though my model is inheriting IInputDto, ICustomValidate and i am overriding AddValidationErrors, the method cannot be reached.

This is my model:

[AutoMap(typeof(DataklasProjectTracking.Core.MasterData.Department))] public class DepartmentModel : IInputDto, ICustomValidate { public int Id { get; set; } public long? TenantId { get; set; } [Required] public string DepartmentName { get; set; } public bool IsActive { get; set; } public string Observations { get; set; } public bool IsDeleted { get; set; } public long? DeletedById { get; set; } public DateTime? DeletedOn { get; set; } public DateTime? UpdatedOn { get; set; } public long? UpdatedById { get; set; } public DateTime CreatedOn { get; set; } public long? CreatedById { get; set; }

    public void AddValidationErrors(List<ValidationResult> results)
    {
        if (DepartmentName.Length < 5)
        {
            results.Add(new ValidationResult("Department name is too short"));
        }
    }

As you can see, i use AutoMapper to convert data from Department entity to my model

Here is Department entity:

[Table("Department")]
public partial class Department : FullAuditedEntity<long>, IMayHaveTenant, IPassivable
{
    public virtual long? TenantId { get; set; }
    public virtual string DepartmentName { get; set; }
    public virtual bool IsActive { get; set; }
    public virtual string Observations { get; set; }

    [ForeignKey("DeletedById")]
    public virtual vwUser DeletedBy { get; set; }

    [ForeignKey("CreatedById")]
    public virtual vwUser CreatedBy { get; set; }

    [ForeignKey("UpdatedById")]
    public virtual vwUser UpdatedBy { get; set; }
}

What is wrong with my code? PS: Sorry for my bad english.

Thank you in advance, Florin Ciubuc


2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Where are you using this DTO? In app services or MVC Controller? Can you share your code.

  • User Avatar
    0
    florinciubuc created

    Problem solved. If someone has this problem,you can verify this:

    This was my flow: My service return EntityClass,which i convert into ModelClass inside the controller. When i create/edit something,i actually edit the model,pass it from the view to the controller. Here,i convert the ModelClass to EntityClass and sent it to the service. The problem is that the validator triggers only when i reach the service. In fact, my service was expecting an EntityClass and i was validating a ModelClass. I modified the code,so the service now expects a ModelClass and i do the conversion right here,not in controller anymore. So,the problem was the fact that i was converting from model to entity before validating.