Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • ReneRam 21 posts 186 karma points
    Apr 06, 2022 @ 08:08
    ReneRam
    0

    BeginUmbracoForm: validation for @Html.CheckBoxFor not returning Error Message

    Hi to everyone. I'm having difficult obtaining the ErrorMesage from my class in a Surface Controller Form. Everything works as expected except for the validation message when a required checkbox must be ticked. I've been trying many things, but the result is always the same: No Error Message.

    Following is part of my Model:

            [Required(ErrorMessage = "L'Oggetto della richiesta è un campo Obbligatorio")]
        [Display(Name = "Oggetto")]
        public string Oggetto { get; set; }
    
        [Required(ErrorMessage = "La Descrizione della richiesta è un campo Obbligatorio")]
        [Display(Name = "Descrizione")]
        public string Descrizione { get; set; }
    
        //[Required(ErrorMessage = "È necessario confermare di aver letto l'informativa sulla privacy.")]
        //[Range(typeof(bool), "false", "true", ErrorMessage = "È necessario confermare di aver letto")]    // funziona a rovescio
        [Range(typeof(bool), "true", "true", ErrorMessage = "È necessario confermare di aver letto l'informativa!")]
        [Display(Name = "Informativa sulla Privacy")]
        public bool Privacy { get; set; }
    

    As you can see I have tried also with the [Required] attribute.

    Following is the View fragment:

        @* Privacy *@
        <div class="row mb-3">
            <div class="col">
                @Html.CheckBoxFor(m => m.Privacy, new { @class = "form-check form-check-inline", @style = "width:25px; height:25px" })
                @Html.LabelFor(m => m.Privacy)<i>&ast;</i>
                @Html.ValidationMessageFor(m => m.Privacy, "", new { @class = "text-danger font-weight-bold blinkingText" })
            </div>
        </div>
    
        @* Submit *@
        <div class="row mb-3">
            <div class="col text-center">
                <input type="submit" value="Invia Richiesta" class="btn btn-cyan" />
            </div>
        </div>
    

    the view fragment is wrapped inside a using (Html.BeginUmbracoForm("Submit", "FormContatti"))

    When I submit with missing fields I get back to the view with all error messages displayed except for the checkbox that has to by ticked:

    enter image description here

    Anyone can help? Thanks René

  • ReneRam 21 posts 186 karma points
    Apr 09, 2022 @ 16:35
    ReneRam
    100

    Found a solution in an old Stack Overflow post:

    MVC Model require true

    In case anyone has the same problem

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies