1

Right now I have something like this in my model class:

        [Required]
        public bool IsNullable { get; set; }

        [Required]
        [StringLength(255, ErrorMessage = "Default value is too long.")]
        public string DefaultValue { get; set; }

I would like to have it so that DefaultValue is only required if isNullable is false. Is there a way to do this? I have been trying to find a solution but still have not found anything useful. Thanks for your time.

  • 1
    This question has been answered before. Please see the link below: https://stackoverflow.com/questions/20642328/how-to-put-conditional-required-attribute-into-class-property-to-work-with-web-a – Auguste Jun 05 '20 at 20:22
  • 1
    You can write your own validation attribute, Auguste has commented or use IValidatableObject for validation (https://stackoverflow.com/questions/56588900/how-to-validate-uploaded-file-in-asp-net-core/56600245#56600245) – Mofaggol Hoshen Jun 05 '20 at 23:52
  • https://github.com/jwaliszko/ExpressiveAnnotations did the job.Thanks for pointing me in the right direction! – Hector Luis Mariscal Jun 08 '20 at 17:55

0 Answers0