Validation fails on fields that have been disabled

By: Jane Onorati | Asked: 04/03/2025
ForumsCategory: Code HelpValidation fails on fields that have been disabled
Jane OnoratiJane Onorati asked 3 weeks ago

I have a form that I disable some fields in using jQuery when an entry is opened for updating.  Then I re-enable the fields when the form is submitted by putting the enable code in an 'on submit' function.  I re-enable the fields because I found that if I do not do this, the form will not submit - the disabled fields fail validation with a 'cannot be blank' error.

This works fine for a variety of field types except fields in a repeater.  My guess is that repeater fields are validated before the 'on submit' event.  I also realize now that if form submittal is halted by a validation error, the other fields are enabled which is what I want to avoid.   So I may need a different approach to re-enabling fields or making them not editable on updates.  I do want them to be editable however on entry creation.

2 Answers
Victor Font Staff answered 3 weeks ago

Repeaters are processed before the parent form. Why don’t you make the fields read only in php before the form is loaded?

Jane OnoratiJane Onorati replied 3 weeks ago

Hi Victor! This is part of a larger script that sets up the form for editing, some of which needs to be dynamic. However I want these particular fields to stay uneditable whenever the form is used to update the user's profile form. I have not found a way to do this with PHP so far, but would be interested in hearing how-to tips if you have any to share. Also, I did a little research and did find I could try setting the fields to read-only with jQuery instead.

Jane OnoratiJane Onorati answered 3 weeks ago

I set the fields to read-only which does not create the issues which setting them to disabled did.  Here is my code:

    function householdMembersPreventEdits (option) {
        // Make repeater household member name and email fields read-only
        const hhAdults = $("[id^=field_cbnu4]:checked").val();
        if (hhAdults === "Yes") { // If household members are included on this membership
            const section_id = "926"; // Household members repeater section ID
            let row_ids = [];
            // Loop through household members rows, use row ids to get element IDs and set to read-only
            $('input[name^="item_meta[' + section_id + '][row_ids]"]').each(function(index, el) {
                row_ids.push(el.value);
                $(document.getElementById(`field_7yvaz-${el.value}`).readOnly = true);
                $(document.getElementById(`field_c5mhf-${el.value}`).readOnly = true);
                $(document.getElementById(`field_9h59r-${el.value}`).readOnly = true);
            });
            $("#frm_field_926_container .frm_repeat_buttons").css("display", "none"); // Hide add and remove row buttons
        }
    }
Victor Font Staff replied 3 weeks ago

Glad you figured it out!

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right