Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Custom Model Validation (IValidatableObject) Not working...

$
0
0

Hello and thanks for reading.  I'm a newbie, trying to learn .net core.  I was learning how to create a custom validator, not using the Validation Attribute but at the class level. Unfortunately, I can't make this one work, please check my code below:  

public class Blog : IValidatableObject
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "Title is required")]
        public string Title { get; set; }
        [Required(ErrorMessage = "Subtitle is required")]
        public string Subtitle { get; set; }

        //[
        //    //ValidarEdad,
        //    Required(AllowEmptyStrings = false, ErrorMessage = "Es mandatorio proveer su fecha de Nacimiento."),
        //    Display(Name = "Fecha de Nacimiento:")
        //    , DataType(DataType.DateTime)
        //]
        public DateTime Nacimiento { get; set; }

        [Required(ErrorMessage = "Url is required"), Display(Name = "URL del Blog:")]
        public string Url { get; set; }
        public string Description { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime ModifiedAt { get; set; }
        public int CreatedBy { get; set; }
        public int ModifiedBy { get; set; }
        public ICollection<Post> Posts { get; set; }

        [Required(ErrorMessage = "Author is required, kindly pick one!"), Display(Name ="Autor:")]
        public int? AuthorId { get; set; }
        public User Author { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {

            Debug.Write("----------------------------");
            Debug.Write("TEST TEST TEST");
            Debug.Write("----------------------------");

            //Blog myblog = (Blog)validationContext.ObjectInstance;          

            //if (Nacimiento > DateTime.Now)
            //{
                yield return new ValidationResult("There's a problem with this date field!", new []{ nameof(Nacimiento) });
            //}

        }
    }

Basically, I have multiple fields but I'm focusing on one field called "Nacimiento" which is a DateTime type, even if I commented on additional code and try to generate an error, nothing happens... I did try to post a message using the Debug.WriteLine to check if this class is doing something but nothing... it basically doesn't do anything, no error when running the validation of the form (server side), and not producing anything, no error messages, no compilation error messages also... please help.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>