Skip to content

Commit e9cdf02

Browse files
Fix display condition bug (D797) (#603)
* Fix for regressive error in display conditions * Modify code to no longer remove erroneous conditions and instead create error * Remove change to typing
1 parent 8ca5bbc commit e9cdf02

3 files changed

Lines changed: 29 additions & 11 deletions

File tree

src/frontend/components/button/lib/submit-field-button.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
/* eslint-disable */
12
import "jstree";
23
import "datatables.net";
34
import "@lol768/jquery-querybuilder-no-eval"
5+
import { validateQueryBuilder } from "validation";
46

57
declare global {
68
interface Window {
@@ -27,6 +29,7 @@ export default class SubmitFieldButton {
2729
*/
2830
constructor(element:JQuery<HTMLElement>) {
2931
element.on('click', (ev) => {
32+
const $form = $(ev.currentTarget).closest('form') as JQuery<HTMLFormElement>;
3033

3134
const $jstreeContainer = $('#field_type_tree');
3235
const $jstreeEl = $('#tree-config .tree-widget-container');
@@ -70,7 +73,9 @@ export default class SubmitFieldButton {
7073
bUpdateFilter = true;
7174
}
7275

73-
if (res && $displayConditionsField.length) {
76+
if(!validateQueryBuilder($displayConditionsBuilderEl)) {
77+
ev.preventDefault();
78+
} else if (res && $displayConditionsField.length) {
7479
bUpdateDisplayConditions = true;
7580
}
7681

@@ -105,7 +110,7 @@ export default class SubmitFieldButton {
105110
window.UpdatePeopleFilter(peopleConditionsFieldEl, ev);
106111
}
107112

108-
if (bUpdateDisplayConditions) {
113+
if (bUpdateDisplayConditions && res) {
109114
$displayConditionsField.val(JSON.stringify(res, null, 2));
110115
}
111116

@@ -115,7 +120,6 @@ export default class SubmitFieldButton {
115120
* and appends them to the form manually */
116121
const $inputs = $permissionTable.DataTable().$('input,select,textarea');
117122
$inputs.hide(); // Stop them appearing to the user in a strange format
118-
const $form = $(ev.currentTarget).closest('form');
119123
$permissionTable.remove();
120124
$form.append($inputs);
121125
});

src/frontend/components/form-group/display-conditions/lib/component.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ class DisplayConditionsComponent extends Component {
2525
{ type: 'contains', accept_values: true, apply_to: ['string'] },
2626
{ type: 'not_equal', accept_values: true, apply_to: ['string'] },
2727
{ type: 'not_contains', accept_values: true, apply_to: ['string'] }
28-
]
29-
})
30-
31-
if (builderData.filterBase) {
32-
const data = JSON.parse(atob(builderData.filterBase))
33-
this.el.queryBuilder('setRules', data)
34-
}
28+
],
29+
allow_empty: true
30+
}).queryBuilder('setRules', builderData.filterBase
31+
? JSON.parse(atob(builderData.filterBase))
32+
: {rules:[]});
3533
}
3634
}
3735

src/frontend/js/lib/validation.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { DangerAlert } from "components/alert/lib/dangerAlert";
2+
13
// Bind events to a field to trigger validation
24
const initValidationOnField = (field) => {
35
// Input
@@ -299,6 +301,20 @@ const validateTree = (field) => {
299301
}
300302
}
301303

304+
const validateQueryBuilder = (field) => {
305+
if(!(field && field.length)) return;
306+
const result = field.queryBuilder('validate');
307+
if(!result) {
308+
if($('.display-conditions-error').length) return false;
309+
const danger = new DangerAlert('There are errors in the conditions you have set, please fix these before submitting the form');
310+
const banner = danger.render();
311+
banner.classList.add('mb-3', 'mt-0', 'display-conditions-error');
312+
field.closest('.card__content').prepend(banner);
313+
return false;
314+
}
315+
return true;
316+
}
317+
302318
// Expand the card with a certain field and scroll it into view
303319
const expandCardValidate = (field) => {
304320
const $collapse = $(field).closest('.card--expandable').find('.collapse')
@@ -321,4 +337,4 @@ const expandCardValidate = (field) => {
321337
}
322338
}
323339

324-
export { initValidationOnField, validateTree, validateRadioGroup, validateCheckboxGroup, validateRequiredFields };
340+
export { initValidationOnField, validateTree, validateRadioGroup, validateCheckboxGroup, validateRequiredFields, validateQueryBuilder };

0 commit comments

Comments
 (0)