From e376ef0242e3e0a6c9359213ddd532995a2d8f6c Mon Sep 17 00:00:00 2001 From: Hakim Razalan Date: Tue, 13 Sep 2022 16:23:54 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20Hidden=20error=20in=20"A?= =?UTF-8?q?dd=20tier"=20modal=20(#15361)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes https://github.com/TryGhost/Ghost/issues/15290 - Capture error from model errors - Add hasValidated for name property to properly mark field as error/success - Add property to hasValidated after each failed validation - Wrap saving on try-catch to suppress uncaught exception (validation error) --- ghost/admin/app/components/modal-tier.hbs | 10 ++++----- ghost/admin/app/components/modal-tier.js | 26 ++++++++++++++--------- ghost/admin/app/validators/tier.js | 2 ++ 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/ghost/admin/app/components/modal-tier.hbs b/ghost/admin/app/components/modal-tier.hbs index 0266be4bbf..838f4afde5 100644 --- a/ghost/admin/app/components/modal-tier.hbs +++ b/ghost/admin/app/components/modal-tier.hbs @@ -14,7 +14,7 @@

Basic

{{#unless this.isFreeTier}} - + - + {{/unless}} - + {{#if this.isFreeTier}} {{/if}} - + {{#unless this.isFreeTier}} @@ -107,7 +107,7 @@ {{/if}} - +

Add a free trial

diff --git a/ghost/admin/app/components/modal-tier.js b/ghost/admin/app/components/modal-tier.js index 45398b8d58..0f01653db0 100644 --- a/ghost/admin/app/components/modal-tier.js +++ b/ghost/admin/app/components/modal-tier.js @@ -1,11 +1,10 @@ -import EmberObject, {action} from '@ember/object'; import ModalBase from 'ghost-admin/components/modal-base'; import TierBenefitItem from '../models/tier-benefit-item'; import classic from 'ember-classic-decorator'; +import {action} from '@ember/object'; import {currencies, getCurrencyOptions, getSymbol} from 'ghost-admin/utils/currency'; import {A as emberA} from '@ember/array'; import {htmlSafe} from '@ember/template'; -import {isEmpty} from '@ember/utils'; import {inject as service} from '@ember/service'; import {task} from 'ember-concurrency'; import {tracked} from '@glimmer/tracking'; @@ -30,7 +29,6 @@ export default class ModalTierPrice extends ModalBase { @tracked stripeMonthlyAmount = 5; @tracked stripeYearlyAmount = 50; @tracked currency = 'usd'; - @tracked errors = EmberObject.create(); @tracked stripePlanError = ''; @tracked benefits = emberA([]); @tracked newBenefit = null; @@ -159,9 +157,7 @@ export default class ModalTierPrice extends ModalBase { @task({drop: true}) *saveTier() { this.validatePrices(); - if (!isEmpty(this.errors) && Object.keys(this.errors).length > 0) { - return; - } + if (this.stripePlanError || this.hasTrialDaysError) { return; } @@ -183,10 +179,20 @@ export default class ModalTierPrice extends ModalBase { } this.tier.set('benefits', this.benefits.filter(benefit => !benefit.get('isBlank'))); - yield this.tier.save(); - this.hasSaved = true; - yield this.confirm(); - this.send('closeModal'); + + try { + yield this.tier.save(); + this.hasSaved = true; + yield this.confirm(); + this.send('closeModal'); + } catch (error) { + if (error === undefined) { + // Validation error + return; + } + + throw error; + } } validatePrices() { diff --git a/ghost/admin/app/validators/tier.js b/ghost/admin/app/validators/tier.js index f17400c093..48b5ac4da4 100644 --- a/ghost/admin/app/validators/tier.js +++ b/ghost/admin/app/validators/tier.js @@ -7,10 +7,12 @@ export default BaseValidator.create({ name(model) { if (!model.name) { model.errors.add('name', 'Please enter Name.'); + model.hasValidated.addObject('name'); this.invalidate(); } if (!validator.isLength(model.name || '', 0, 191)) { model.errors.add('name', 'Name cannot be longer than 191 characters.'); + model.hasValidated.addObject('name'); this.invalidate(); } }