0

EE v2.11.1
Entry Type v1.1.4

I'm using Entry Types to show/hide fields based on a selection. What I would like to do is show the fields and make some of them required if the admin user selects Yes on Entry Type pill button. Currently, if I set fields to required and the Entry Type pill button has not been set to "Yes", the entry will not publish, because the hidden fields are required.

Ideally, I would like to use the CP CSS & JS add-on to add required classes to certain fields based on the value of the Entry Type pill button. Is this even possible? If not, has anyone run into this before? If so, could you please share how solved it?

0
1

You can use jQuery to provide additional validation for the Channel Entry form using the CP CSS & JS add-on. You'll want to add some JavaScript to your entry form that hooks required attributes to the specific fields you want to modify on the fly.

Note: This will not provide backend validation, just frontend. So technically, if you conditionally add the required attribute to an input, the enterer could use their browser's developer tools to simply remove that attribute and the form would submit.

Here is an example of adding the attribute with jQuery to a form input:

$("#conditionally_required_input").prop('required',true);

And there a several ways of triggering that by adding an event listener to the trigger element:

$("#triggers_conditionally_required_input").click(function() {
    if ($(this).val() == "Yes") {
        // use jQuery to add the required prop to some element
    } else {
        // similar, but here you want to remove required prop (set false)
    }
});

There isn't any practical way to add back-end validation on the fly (complicated add-on mess), and it would create unusable behaviour were the enterer would open an entry and have an unexpected field be required because the last enterer made certain choices.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.