locked
Blazor Validation RRS feed

  • Question

  • User-941767363 posted

    Hi, is it possible to "Turn on/turn off" RUNTIME the [Required] attribute of a member of a class?

    For example I need to "turn off" the Required of "TemperatureC" in a specific situation (when I click on a specific button).

    <div>    public class WeatherForecast</div> <div>    {</div> <div>        public DateTime Date { get; set; }</div> <div>        [Required]</div> <div>        public int? TemperatureC { get; set; }</div> <div> </div> <div>        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);</div> <div>        [Required]</div> <div>        [MaxLength(10)]</div> <div>        public string Summary { get; set; }</div> <div>    }</div>

    Thursday, July 2, 2020 4:02 PM

Answers

  • User-821857111 posted

    is it possible to "Turn on/turn off" RUNTIME the [Required] attribute of a member of a class?
    No. You should remove the attribute if it is not always required and  validate the property value depending on the circumstances.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 3, 2020 7:33 AM

All replies

  • User-821857111 posted

    is it possible to "Turn on/turn off" RUNTIME the [Required] attribute of a member of a class?
    No. You should remove the attribute if it is not always required and  validate the property value depending on the circumstances.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 3, 2020 7:33 AM
  • User-941767363 posted

    OK, thank you for the reply.

    Best

    Stefano

    Friday, July 3, 2020 8:00 AM