terminal42 / contao-conditionalformfields

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

Field named `function` causes JavaScript error #82

Closed fritzmg closed 7 months ago

fritzmg commented 11 months ago

If you have a field named function in your Contao form ("function" of a person within an organisation), the following error will occur:

Uncaught SyntaxError: missing variable name
    toggleFieldset /bundles/terminal42conditionalformfields/conditionalformfields.js?v=0347389d:42
    init /bundles/terminal42conditionalformfields/conditionalformfields.js?v=0347389d:23
    init /bundles/terminal42conditionalformfields/conditionalformfields.js?v=0347389d:7
    load /bundles/terminal42conditionalformfields/conditionalformfields.js?v=0347389d:102

This is because function is a reserved keyword, but conditionalformfields.js produces JavaScript code like this for this field:

https://github.com/terminal42/contao-conditionalformfields/blob/06dd538eb6c376e2ec88da75a7560145d51315bd/public/conditionalformfields.js#L36

const function = values.get('function');

This is illegal as function is a reserved keyword.

May be the condition can be checked for reserved keywords and prevent saving in such a case. Also additionally may be rather than creating a variable for every form field only the variables used in the condition could be added to the Function (thus making such collisions less likely - at least in our case the function field was not actually used in any condition).

aschempp commented 7 months ago

Validation added in e85bb9e0f63ff4f1c12b80fb322242ed9e8e6558