Template to toggle what fields are required, to disable button and similar

Hi everyone,

I wanted to share a useful JavaScript function that you can use as a template for ensuring all required fields are filled out before doing something; whether that’s part of a form or something else where you’re not using a form. This function can dynamically adjust which fields are required based on certain conditions and controls the disable state of the submit button accordingly.

You can create a formula for this, such as “shouldDisableButton()” in components (or multiple of these if you’re using them outside components, e.g. “shouldDisableButton1()… shouldDisableButton2()” then just put it directly in your button’s “disabled” binding.

I have two versions below: The first allows conditional logic, the second is simplified and just checks true/false.

What This Function Does:

  1. Initializes Variables: First define names for the variables (and make them = WeWeb variables)
  2. Applies Conditional Requirements: It adjusts the required status of certain fields based on specified conditions.
  3. Validates Required Fields: It checks if all required fields have values and returns a boolean to control the disable state of a submit button.

Here’s a generic and easily modifiable version of the function:

/*
This function ensures that all required form fields are filled before allowing submission. It adjusts required fields based on conditions and controls the disable state of a button.
*/

// Define variables at the top with generic placeholders; you can rename them as you like
const variable01 = variables['01WeWebUuidGoesHere-value']; // Variable 01 - value
const variable02 = variables['02WeWebUuidGoesHere-value']; // Variable 02 - value
const variable03 = variables['03WeWebUuidGoesHere-value']; // Variable 03 - value
const variable04 = variables['04WeWebUuidGoesHere-value']; // Variable 04 - value
const variable05 = variables['05WeWebUuidGoesHere-value']; // Variable 05 - value

// Variables and their "required" status - you can change "required" to true/false as needed to mark whether or not the function considers them as required.

const variablesWithStatus = {
    variable01: { value: variable01, required: false }, // Variable 01 name
    variable02: { value: variable02, required: true },  // Variable 02 name
    variable03: { value: variable03, required: true },  // Variable 03 name
    variable04: { value: variable04, required: false }, // Variable 04 name
    variable05: { value: variable05, required: true },  // Variable 05 name
};

// Function to apply conditional requirements
const applyConditionalRequirements = (variables) => {
    // Example condition: if variable05 equals 197, set variable03 as required
    if (variables.variable05.value === 197) {
        variables.variable03.required = true;
    }
    // Example condition: if variable02 equals 'other', set variable03 as required
    if (variables.variable02.value === 'other') {
        variables.variable03.required = true;
    }
    // Add more conditional requirements as needed
};

// Apply the conditional requirements
applyConditionalRequirements(variablesWithStatus);

// Check if all required variables have truthy values
let allRequiredAreTruthy = true;

// Loop through each variable to check if required ones have values
for (let key in variablesWithStatus) {
    // If a variable is required and it doesn't have a value, set allRequiredAreTruthy to false
    if (variablesWithStatus[key].required && !variablesWithStatus[key].value) {
        allRequiredAreTruthy = false;
        break; // Exit the loop early if a required value is missing
    }
}

// Invert the result: return false if all are truthy and true if any are not
// because this is used in a disable button which expects true to disable
return !allRequiredAreTruthy; // Output: true or false

Simplified Version without Conditional Logic

For those who need a simpler example without conditional requirements and with only three variables, here’s an alternative version:

/*
This function ensures that all required form fields are filled before allowing submission.
*/

// Define variables at the top with generic placeholders
const variable01 = variables['01WeWebUuidGoesHere-value']; // Variable 01 - value
const variable02 = variables['02WeWebUuidGoesHere-value']; // Variable 02 - value
const variable03 = variables['03WeWebUuidGoesHere-value']; // Variable 03 - value

// Variables and their "required" status
const variablesWithStatus = {
    variable01: { value: variable01, required: true }, // Variable 01 name
    variable02: { value: variable02, required: true },  // Variable 02 name
    variable03: { value: variable03, required: true }   // Variable 03 name
};

// Check if all required variables have truthy values
let allRequiredAreTruthy = true;

// Loop through each variable to check if required ones have values
for (let key in variablesWithStatus) {
    // If a variable is required and it doesn't have a value, set allRequiredAreTruthy to false
    if (variablesWithStatus[key].required && !variablesWithStatus[key].value) {
        allRequiredAreTruthy = false;
        break; // Exit the loop early if a required value is missing
    }
}

// Invert the result: return false if all are truthy and true if any are not
// because this is used in a disable button which expects true to disable
return !allRequiredAreTruthy; // Output: true or false

4 Likes

Cool! Thanks @Matthew :grinning:

Do you think it would make sense to abstract something like this inside a component for the Starter Kit?