Page 1 of 1

Hide fields with "required" attribute (v2023)

Posted: Fri Jun 21, 2024 7:31 pm
by gbellabarba

Hi, I have set some required fields which are hidden based on some choices. initially everything worked automatically but then suddenly when I go to add a record, it started asking me for the required but hidden and empty fields. I set the required field by code but it doesn't work.


Re: Required fields (v2023)

Posted: Fri Jun 21, 2024 8:33 pm
by arbei

gbellabarba wrote:

I set the required field by code

You may post your code for discussion.


Re: Hide required fields (v2023)

Posted: Fri Jun 21, 2024 8:42 pm
by gbellabarba
$(document).ready(function() {

    function toggleIdLargeAreaUnocHabitat() {
        $("#r_id_large_area_unoc_habitat").attr("required", "required");;
        if ($("#x_id_area_suf_long_tern").val() == "1" || $("#x_id_area_suf_long_tern").val() == "3") {
            $("#r_id_large_area_unoc_habitat").hide();
            //$("#x_id_large_area_unoc_habitat").prop("required", false);
            $("#x_id_large_area_unoc_habitat").removeAttr("required");
            $("#x_id_large_area_unoc_habitat").val("").trigger('change');
        } else {
            $("#r_id_large_area_unoc_habitat").show();
            //$("#x_id_large_area_unoc_habitat").prop("required", true);
            $("#x_id_large_area_unoc_habitat").attr("required", "required");
        }
    }

    
    toggleIdLargeAreaUnocHabitat();

   $("#x_id_area_suf_long_tern").change(function() {
        toggleIdLargeAreaUnocHabitat();
    });
});

Re: Hide required fields (v2023)

Posted: Sat Jun 22, 2024 11:07 am
by arbei

You JavaScript code should check if the field is hidden before adding the "required" attribute, see :hidden Selector.