Feature Request - Conditional Required

7 posts by 3 authors in: Forums > CMS Builder
Last Post: December 15   (RSS)

By KennyH - October 18, 2023

Something that seems to be coming up more and more is needing a way to make a field required, but only if another field is checked or has content.

For example, let's say we have the following fields:

  • Warranty Serviced (checkbox)
  • Date of Service (date/time)
  • Repair Type (dropdown)
  • Description (wysiwyg)

I want these three fields to be required ONLY if Warranty Serviced has been checked:

  • Date of Service (date/time)
  • Repair Type (dropdown)
  • Description (wysiwyg)

If Warranty Serviced has not been checked, then none of those fields would be required to have content.

What do you think?

Kenny H

By Dave - October 19, 2023

Hi Kenny, 

You could do it with a plugin, check out the attached code.  

Give it a try and let me know any questions.  You'll need to make sure the tablename and field names match.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By KennyH - October 19, 2023

Amazing! This works perfectly!

I changed the name of the file and plugin so if I ever needed to add more than one it should work without conflict. However, shouldn't I just be able to add more tables / fields to this plugin? 

Thanks so much for putting this together.

Kenny H

By Dave - October 19, 2023

Hi Kenny, 

Ok great, glad that's working!

Yes, could can just modify the code a bit to add additional tables.  Here's an example:

function conditionalErrorChecking($tableName, $recordExists, $oldRecord) {
    $errors = [];

    // table 1
    if ($tableName === 'your_table_here') {

        // conditional error checking rules
        $isWarrantyServiced = !empty($_REQUEST['warranty_serviced']);
        if ($isWarrantyServiced) {
            if (empty($_REQUEST['date_of_service:string'])) { // add :string prefix to date entire date string (if set)
                $errors[] = "Please fill out the date of service field.";
            }
            if (empty($_REQUEST['repair_type'])) {
                $errors[] = "Please fill out the repair type field.";
            }
            $wysiwygWithoutTagsOrWhitespace = trim(strip_tags($_REQUEST['description'] ?? ''));
            if (empty($wysiwygWithoutTagsOrWhitespace)) { // wysiwyg, remove all tags and check if empty after that
                $errors[] = "Please fill out the description type field.";
            }
        }
    }

    // table 2
    if ($tableName === 'another_table') {
        // error checking code goes here
    }

    // show errors
    if ($errors) {
        $errorsHTML = implode("\n", $errors);
        die($errorsHTML);
    }
}

I previously added some extra variables for readability, they're not strictly necessary.  

Let me know if you have any questions about how it works or how to add another table/field.

Dave Edis - Senior Developer
interactivetools.com

By Dave - October 21, 2023

Hi Kenny, 

That looks great, nice work and thanks for sharing!

Dave Edis - Senior Developer
interactivetools.com

By kitsguru - December 15

This is great, Thanks for sharing. I have several use cases for this. I am adding it to my arsenal.

Jeff Shields