summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkialam <digitalanime@gmail.com>2018-08-06 19:54:49 +0200
committerGitHub <noreply@github.com>2018-08-06 19:54:49 +0200
commit4800b4455feb1958a78f9fd0977a249ef1863cea (patch)
treed9853ba0e64af79e0b746cd3f94a0419ca2de809
parentMerge pull request #2762 from ryanpetrello/fix-2759 (diff)
parentRestore missing API form errors for selects. (diff)
downloadawx-4800b4455feb1958a78f9fd0977a249ef1863cea.tar.xz
awx-4800b4455feb1958a78f9fd0977a249ef1863cea.zip
Merge pull request #2753 from kialam/fix/1475-long-label-error
Client-side validation of Label field in Job Template form
-rw-r--r--awx/ui/client/src/shared/form-generator.js14
-rw-r--r--awx/ui/client/src/templates/job_templates/add-job-template/job-template-add.controller.js46
-rw-r--r--awx/ui/client/src/templates/job_templates/edit-job-template/job-template-edit.controller.js46
-rw-r--r--awx/ui/client/src/templates/job_templates/job-template.form.js4
4 files changed, 110 insertions, 0 deletions
diff --git a/awx/ui/client/src/shared/form-generator.js b/awx/ui/client/src/shared/form-generator.js
index b18e9a5397..f9e1b9e426 100644
--- a/awx/ui/client/src/shared/form-generator.js
+++ b/awx/ui/client/src/shared/form-generator.js
@@ -1041,6 +1041,15 @@ angular.module('FormGenerator', [GeneratorHelpers.name, 'Utilities', listGenerat
};
}
+ if (field.onError) {
+ labelOptions.onError = {
+ id: `${this.form.name}_${fld}_error_text`,
+ ngShow: field.onError.ngShow,
+ ngModel: field.onError.variable,
+ text: field.onError.text
+ };
+ }
+
html += label(labelOptions);
html += "<div ";
@@ -1091,7 +1100,11 @@ angular.module('FormGenerator', [GeneratorHelpers.name, 'Utilities', listGenerat
}
html += "</div>\n";
}
+ if (field.label === "Labels") {
+ html += `<div class="error" id="${field.onError.id}" ng-show="${field.onError.ngShow}">${field.onError.text}</div>`;
+ }
html += "<div class=\"error api-error\" id=\"" + this.form.name + "-" + fld + "-api-error\" ng-bind=\"" + fld + "_api_error\"></div>\n";
+
// Add help panel(s)
html += (field.helpCollapse) ? this.buildHelpCollapse(field.helpCollapse) : '';
@@ -2021,5 +2034,6 @@ angular.module('FormGenerator', [GeneratorHelpers.name, 'Utilities', listGenerat
${options.text}
</label> `;
}
+
}
]);
diff --git a/awx/ui/client/src/templates/job_templates/add-job-template/job-template-add.controller.js b/awx/ui/client/src/templates/job_templates/add-job-template/job-template-add.controller.js
index d25d41e706..a51213251d 100644
--- a/awx/ui/client/src/templates/job_templates/add-job-template/job-template-add.controller.js
+++ b/awx/ui/client/src/templates/job_templates/add-job-template/job-template-add.controller.js
@@ -495,5 +495,51 @@
$scope.formCancel = function () {
$state.transitionTo('templates');
};
+
+ let handleLabelCount = () => {
+ /**
+ * This block of code specifically handles the client-side validation of the `labels` field.
+ * Due to it's detached nature in relation to the other job template fields, we must
+ * validate this field client-side in order to avoid the edge case where a user can make a
+ * successful POST to the `job_templates` endpoint but however encounter a 200 error from
+ * the `labels` endpoint due to a character limit.
+ *
+ * We leverage two of select2's available events, `select` and `unselect`, to detect when the user
+ * has either added or removed a label. From there, we set a flag and do simple string length
+ * checks to make sure a label's chacacter count remains under 512. Otherwise, we disable the "Save" button
+ * by invalidating the field and inform the user of the error.
+ */
+
+
+ $scope.job_template_labels_isValid = true;
+ const maxCount = 512;
+ const jt_label_id = 'job_template_labels';
+
+ // Detect when a new label is added
+ $(`#${jt_label_id}`).on('select2:select', (e) => {
+ const { text } = e.params.data;
+
+ // If the character count of an added label is greater than 512, we set `labels` field as invalid
+ if (text.length > maxCount) {
+ $scope.job_template_form.labels.$setValidity(`${jt_label_id}`, false);
+ $scope.job_template_labels_isValid = false;
+ }
+ });
+
+ // Detect when a label is removed
+ $(`#${jt_label_id}`).on('select2:unselect', (e) => {
+ const maxCount = 512;
+ const { text } = e.params.data;
+
+ /* If the character count of a removed label is greater than 512 AND the field is currently marked
+ as invalid, we set it back to valid */
+ if (text.length > maxCount && $scope.job_template_form.labels.$error) {
+ $scope.job_template_form.labels.$setValidity(`${jt_label_id}`, true);
+ $scope.job_template_labels_isValid = true;
+ }
+ });
+ };
+
+ handleLabelCount();
}
];
diff --git a/awx/ui/client/src/templates/job_templates/edit-job-template/job-template-edit.controller.js b/awx/ui/client/src/templates/job_templates/edit-job-template/job-template-edit.controller.js
index 41528a57d8..cf5977baa3 100644
--- a/awx/ui/client/src/templates/job_templates/edit-job-template/job-template-edit.controller.js
+++ b/awx/ui/client/src/templates/job_templates/edit-job-template/job-template-edit.controller.js
@@ -704,5 +704,51 @@ export default
$scope.formCancel = function () {
$state.go('templates');
};
+
+ let handleLabelCount = () => {
+ /**
+ * This block of code specifically handles the client-side validation of the `labels` field.
+ * Due to it's detached nature in relation to the other job template fields, we must
+ * validate this field client-side in order to avoid the edge case where a user can make a
+ * successful POST to the `job_templates` endpoint but however encounter a 200 error from
+ * the `labels` endpoint due to a character limit.
+ *
+ * We leverage two of select2's available events, `select` and `unselect`, to detect when the user
+ * has either added or removed a label. From there, we set a flag and do simple string length
+ * checks to make sure a label's chacacter count remains under 512. Otherwise, we disable the "Save" button
+ * by invalidating the field and inform the user of the error.
+ */
+
+
+ $scope.job_template_labels_isValid = true;
+ const maxCount = 512;
+ const jt_label_id = 'job_template_labels';
+
+ // Detect when a new label is added
+ $(`#${jt_label_id}`).on('select2:select', (e) => {
+ const { text } = e.params.data;
+
+ // If the character count of an added label is greater than 512, we set `labels` field as invalid
+ if (text.length > maxCount) {
+ $scope.job_template_form.labels.$setValidity(`${jt_label_id}`, false);
+ $scope.job_template_labels_isValid = false;
+ }
+ });
+
+ // Detect when a label is removed
+ $(`#${jt_label_id}`).on('select2:unselect', (e) => {
+ const maxCount = 512;
+ const { text } = e.params.data;
+
+ /* If the character count of a removed label is greater than 512 AND the field is currently marked
+ as invalid, we set it back to valid */
+ if (text.length > maxCount && $scope.job_template_form.labels.$error) {
+ $scope.job_template_form.labels.$setValidity(`${jt_label_id}`, true);
+ $scope.job_template_labels_isValid = true;
+ }
+ });
+ };
+
+ handleLabelCount();
}
];
diff --git a/awx/ui/client/src/templates/job_templates/job-template.form.js b/awx/ui/client/src/templates/job_templates/job-template.form.js
index d87d3c603e..7b75e16415 100644
--- a/awx/ui/client/src/templates/job_templates/job-template.form.js
+++ b/awx/ui/client/src/templates/job_templates/job-template.form.js
@@ -229,6 +229,10 @@ function(NotificationsList, i18n) {
dataPlacement: 'right',
awPopOver: "<p>" + i18n._("Optional labels that describe this job template, such as 'dev' or 'test'. Labels can be used to group and filter job templates and completed jobs.") + "</p>",
dataContainer: 'body',
+ onError: {
+ ngShow: 'job_template_labels_isValid !== true',
+ text: i18n._('Max 512 characters per label.'),
+ },
ngDisabled: '!(job_template_obj.summary_fields.user_capabilities.edit || canAddJobTemplate)'
},
custom_virtualenv: {