Skip to content

Commit

Permalink
Restored onPreProcess and validation code
Browse files Browse the repository at this point in the history
Even though the UI no longer allows invalid answers since it doesn't support free text,
it's still possible for another question to update a combobox with an invalid answer.
  • Loading branch information
orangejenny committed Nov 7, 2020
1 parent ac807f5 commit 36ac455
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,9 @@ hqDefine("cloudcare/js/form_entry/entrycontrols_full", function () {

self.options.subscribe(function () {
self.renderSelect2();
if (!self.isValid(self.rawAnswer())) {
self.question.error(gettext('Not a valid choice'));
}
});

self.additionalSelect2Options = function () {
Expand Down Expand Up @@ -614,7 +617,7 @@ hqDefine("cloudcare/js/form_entry/entrycontrols_full", function () {
if (!value) {
return true;
}
return _.contains(self.choices(), value);
return _.contains(_.pluck(self.options(), 'text'), value);
};

self.enableReceiver(question, options);
Expand Down Expand Up @@ -656,6 +659,24 @@ hqDefine("cloudcare/js/form_entry/entrycontrols_full", function () {

ComboboxEntry.prototype = Object.create(DropdownEntry.prototype);
ComboboxEntry.prototype.constructor = DropdownEntry;
ComboboxEntry.prototype.onPreProcess = function (newValue) {
var value;
if (newValue === Const.NO_ANSWER || newValue === '') {
this.answer(Const.NO_ANSWER);
this.question.error(null);
return;
}

value = _.find(this.options(), function (d) {
return d.text === newValue;
});
if (value) {
this.answer(value.id);
this.question.error(null);
} else {
this.question.error(gettext('Not a valid choice'));
}
};
ComboboxEntry.prototype.receiveMessage = function (message, field) {
// Iterates through options and selects an option that matches message[field].
// Registers a no answer if message[field] is not in options.
Expand Down

0 comments on commit 36ac455

Please sign in to comment.