From f548e36ad72b3df04bd3bbad1f70de176fa26875 Mon Sep 17 00:00:00 2001 From: mabashian Date: Tue, 7 Aug 2018 13:53:12 -0400 Subject: [PATCH 1/4] Propagate label length validation to workflow form --- .../job-template-add.controller.js | 77 +++++++++---------- .../job-template-edit.controller.js | 77 +++++++++---------- awx/ui/client/src/templates/workflows.form.js | 4 + .../add-workflow/workflow-add.controller.js | 37 +++++++++ .../edit-workflow/workflow-edit.controller.js | 37 +++++++++ 5 files changed, 150 insertions(+), 82 deletions(-) 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 a51213251d..e933bc5cda 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 @@ -496,50 +496,45 @@ $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. - */ + /** + * 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'; - $scope.job_template_labels_isValid = true; + // 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 jt_label_id = 'job_template_labels'; + const { text } = e.params.data; - // 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(); + /* 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; + } + }); } ]; 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 cf5977baa3..b9925ad814 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 @@ -705,50 +705,45 @@ export default $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. - */ + /** + * 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'; - $scope.job_template_labels_isValid = true; + // 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 jt_label_id = 'job_template_labels'; + const { text } = e.params.data; - // 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(); + /* 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; + } + }); } ]; diff --git a/awx/ui/client/src/templates/workflows.form.js b/awx/ui/client/src/templates/workflows.form.js index 104447bae4..50a3ee867c 100644 --- a/awx/ui/client/src/templates/workflows.form.js +++ b/awx/ui/client/src/templates/workflows.form.js @@ -77,6 +77,10 @@ export default ['NotificationsList', 'i18n', function(NotificationsList, i18n) { dataPlacement: 'right', awPopOver: "

" + 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.") + "

", dataContainer: 'body', + onError: { + ngShow: 'workflow_job_template_labels_isValid !== true', + text: i18n._('Max 512 characters per label.'), + }, ngDisabled: '!(workflow_job_template_obj.summary_fields.user_capabilities.edit || canAddWorkflowJobTemplate)' }, variables: { diff --git a/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js b/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js index 8321e839f9..84d911253f 100644 --- a/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js +++ b/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js @@ -178,5 +178,42 @@ export default [ $scope.formCancel = function () { $state.transitionTo('templates'); }; + + /** + * 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.workflow_job_template_labels_isValid = true; + const maxCount = 512; + const wfjt_label_id = 'workflow_job_template_labels'; + // Detect when a new label is added + $(`#${wfjt_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.workflow_job_template_form.labels.$setValidity(`${wfjt_label_id}`, false); + $scope.workflow_job_template_labels_isValid = false; + } + }); + // Detect when a label is removed + $(`#${wfjt_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.workflow_job_template_form.labels.$error) { + $scope.workflow_job_template_form.labels.$setValidity(`${wfjt_label_id}`, true); + $scope.workflow_job_template_labels_isValid = true; + } + }); } ]; diff --git a/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js b/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js index 506ca9ba62..a6aa1ad6dc 100644 --- a/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js +++ b/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js @@ -322,5 +322,42 @@ export default [ $scope.survey_exists = true; $scope.invalid_survey = false; }); + + /** + * 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.workflow_job_template_labels_isValid = true; + const maxCount = 512; + const wfjt_label_id = 'workflow_job_template_labels'; + // Detect when a new label is added + $(`#${wfjt_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.workflow_job_template_form.labels.$setValidity(`${wfjt_label_id}`, false); + $scope.workflow_job_template_labels_isValid = false; + } + }); + // Detect when a label is removed + $(`#${wfjt_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.workflow_job_template_form.labels.$error) { + $scope.workflow_job_template_form.labels.$setValidity(`${wfjt_label_id}`, true); + $scope.workflow_job_template_labels_isValid = true; + } + }); } ]; From eb8c1d2c7874150aae51daed8058b1a89315778b Mon Sep 17 00:00:00 2001 From: mabashian Date: Tue, 7 Aug 2018 13:55:15 -0400 Subject: [PATCH 2/4] Fix comment --- .../workflows/add-workflow/workflow-add.controller.js | 4 ++-- .../workflows/edit-workflow/workflow-edit.controller.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js b/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js index 84d911253f..37a0749e3a 100644 --- a/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js +++ b/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js @@ -183,7 +183,7 @@ export default [ * 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 + * successful POST to the `workflow_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 @@ -191,7 +191,7 @@ export default [ * 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.workflow_job_template_labels_isValid = true; const maxCount = 512; const wfjt_label_id = 'workflow_job_template_labels'; diff --git a/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js b/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js index a6aa1ad6dc..772fc75f13 100644 --- a/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js +++ b/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js @@ -327,7 +327,7 @@ export default [ * 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 + * successful POST to the `workflow_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 @@ -335,7 +335,7 @@ export default [ * 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.workflow_job_template_labels_isValid = true; const maxCount = 512; const wfjt_label_id = 'workflow_job_template_labels'; From c7466ce44906b6130ae5f46e3969afdf573059ed Mon Sep 17 00:00:00 2001 From: mabashian Date: Tue, 7 Aug 2018 14:27:04 -0400 Subject: [PATCH 3/4] Wraps label count validation in function --- .../job-template-add.controller.js | 77 +++++++++--------- .../job-template-edit.controller.js | 78 ++++++++++--------- .../add-workflow/workflow-add.controller.js | 72 +++++++++-------- .../edit-workflow/workflow-edit.controller.js | 72 +++++++++-------- 4 files changed, 158 insertions(+), 141 deletions(-) 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 e933bc5cda..f343a10ac0 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 @@ -496,45 +496,50 @@ $state.transitionTo('templates'); }; - /** - * 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. - */ + 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) => { + $scope.job_template_labels_isValid = true; const maxCount = 512; - const { text } = e.params.data; + const jt_label_id = 'job_template_labels'; - /* 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; - } - }); + // 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 b9925ad814..c300ee2c73 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 @@ -705,45 +705,49 @@ export default $state.go('templates'); }; - /** - * 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. - */ + 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) => { + $scope.job_template_labels_isValid = true; const maxCount = 512; - const { text } = e.params.data; + const jt_label_id = 'job_template_labels'; - /* 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; - } - }); + // 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/workflows/add-workflow/workflow-add.controller.js b/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js index 37a0749e3a..82d85d8168 100644 --- a/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js +++ b/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js @@ -179,41 +179,45 @@ export default [ $state.transitionTo('templates'); }; - /** - * 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 `workflow_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. - */ + 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 `workflow_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.workflow_job_template_labels_isValid = true; - const maxCount = 512; - const wfjt_label_id = 'workflow_job_template_labels'; - // Detect when a new label is added - $(`#${wfjt_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.workflow_job_template_form.labels.$setValidity(`${wfjt_label_id}`, false); - $scope.workflow_job_template_labels_isValid = false; - } - }); - // Detect when a label is removed - $(`#${wfjt_label_id}`).on('select2:unselect', (e) => { + $scope.workflow_job_template_labels_isValid = true; 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.workflow_job_template_form.labels.$error) { - $scope.workflow_job_template_form.labels.$setValidity(`${wfjt_label_id}`, true); - $scope.workflow_job_template_labels_isValid = true; - } - }); + const wfjt_label_id = 'workflow_job_template_labels'; + // Detect when a new label is added + $(`#${wfjt_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.workflow_job_template_form.labels.$setValidity(`${wfjt_label_id}`, false); + $scope.workflow_job_template_labels_isValid = false; + } + }); + // Detect when a label is removed + $(`#${wfjt_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.workflow_job_template_form.labels.$error) { + $scope.workflow_job_template_form.labels.$setValidity(`${wfjt_label_id}`, true); + $scope.workflow_job_template_labels_isValid = true; + } + }); + }; + + handleLabelCount(); } ]; diff --git a/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js b/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js index 772fc75f13..0093637068 100644 --- a/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js +++ b/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js @@ -323,41 +323,45 @@ export default [ $scope.invalid_survey = false; }); - /** - * 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 `workflow_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. - */ + 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 `workflow_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.workflow_job_template_labels_isValid = true; - const maxCount = 512; - const wfjt_label_id = 'workflow_job_template_labels'; - // Detect when a new label is added - $(`#${wfjt_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.workflow_job_template_form.labels.$setValidity(`${wfjt_label_id}`, false); - $scope.workflow_job_template_labels_isValid = false; - } - }); - // Detect when a label is removed - $(`#${wfjt_label_id}`).on('select2:unselect', (e) => { + $scope.workflow_job_template_labels_isValid = true; 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.workflow_job_template_form.labels.$error) { - $scope.workflow_job_template_form.labels.$setValidity(`${wfjt_label_id}`, true); - $scope.workflow_job_template_labels_isValid = true; - } - }); + const wfjt_label_id = 'workflow_job_template_labels'; + // Detect when a new label is added + $(`#${wfjt_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.workflow_job_template_form.labels.$setValidity(`${wfjt_label_id}`, false); + $scope.workflow_job_template_labels_isValid = false; + } + }); + // Detect when a label is removed + $(`#${wfjt_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.workflow_job_template_form.labels.$error) { + $scope.workflow_job_template_form.labels.$setValidity(`${wfjt_label_id}`, true); + $scope.workflow_job_template_labels_isValid = true; + } + }); + }; + + handleLabelCount(); } ]; From 273754dc79811610b9ad1d611c07ec63937c51a1 Mon Sep 17 00:00:00 2001 From: mabashian Date: Tue, 7 Aug 2018 16:17:20 -0400 Subject: [PATCH 4/4] Removes redundant maxCount declaration --- .../add-job-template/job-template-add.controller.js | 1 - .../edit-job-template/job-template-edit.controller.js | 1 - .../templates/workflows/add-workflow/workflow-add.controller.js | 1 - .../workflows/edit-workflow/workflow-edit.controller.js | 1 - 4 files changed, 4 deletions(-) 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 f343a10ac0..1ee7f244ea 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 @@ -528,7 +528,6 @@ // 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 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 c300ee2c73..1b6b4a1c99 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 @@ -736,7 +736,6 @@ export default // 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 diff --git a/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js b/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js index 82d85d8168..84bfa0186d 100644 --- a/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js +++ b/awx/ui/client/src/templates/workflows/add-workflow/workflow-add.controller.js @@ -207,7 +207,6 @@ export default [ }); // Detect when a label is removed $(`#${wfjt_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 */ diff --git a/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js b/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js index 0093637068..dc6a35d402 100644 --- a/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js +++ b/awx/ui/client/src/templates/workflows/edit-workflow/workflow-edit.controller.js @@ -351,7 +351,6 @@ export default [ }); // Detect when a label is removed $(`#${wfjt_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 */