terminal42 / contao-conditionalformfields

Display form fields conditionally in Contao Open Source CMS
MIT License
18 stars 12 forks source link

Javascript doesn't work if input arrays are used #66

Closed theDyingMountain closed 1 year ago

theDyingMountain commented 2 years ago

If input arrays are used, the const declaration fails, because of the name is not unique, e.g. const arrayInput[1][field1] = values.get('arrayInput[1][field1]'); const arrayInput[1][field2] = values.get('arrayInput[1][field2]');

My current workaround:

    let counter = 0;
    formData.forEach(function (value, key) {
        if (String(key).includes('-')) {
            console.warn(`terminal42/contao-conditionalformfields: skipping "${key}", special characters [-] are not supported in JavaScript variables.`);
        } else {
            if (String(key).includes('[')) {
                let varName = key.substring(0, key.indexOf('['));
                fnBody += `const ${varName + counter} = values.get('${key}');`;
            } else {
                fnBody += `const ${key} = values.get('${key}');`;
            }

        }
        counter++;
    });
aschempp commented 1 year ago

06dd538eb6c376e2ec88da75a7560145d51315bd now skips array variables, because setting them to a "random number" makes no sense, you still wouldn't be able to use them in your conditions imho. Fields that are needed in conditions need to have a valid/supported name.