From 60f690dad042d1068223b2b88e196c901aa94fff Mon Sep 17 00:00:00 2001 From: Joe Gaudet Date: Tue, 28 Feb 2023 12:16:54 -0800 Subject: [PATCH] Add required text to labels based on the model This commit adds the ability for the form to represent required fields based on the validations present on the model. Currently only supports ember-model-validators, but could be extended easily through the required attribute. --- addon/components/field-for.hbs | 9 ++++- addon/components/field-for.js | 4 ++- addon/components/form-for.js | 10 ++++++ addon/components/label-for.hbs | 8 +++-- addon/components/label-for.js | 16 ++++++++- addon/services/form-for.js | 2 ++ config/environment.js | 2 ++ tests/dummy/app/templates/docs/forms.md | 6 ++++ tests/dummy/app/templates/docs/index.md | 8 +++++ tests/dummy/config/environment.js | 1 + tests/integration/components/form-for-test.js | 35 +++++++++++++++++++ 11 files changed, 96 insertions(+), 5 deletions(-) diff --git a/addon/components/field-for.hbs b/addon/components/field-for.hbs index 4d21315a..8f8d9aeb 100644 --- a/addon/components/field-for.hbs +++ b/addon/components/field-for.hbs @@ -22,7 +22,14 @@ {{did-insert this.registerElement}} > {{#unless this.hideDefaultLabel}} - + {{/unless}} {{#if this._showValue}} {{#if this.displayValueComponent}} diff --git a/addon/components/field-for.js b/addon/components/field-for.js index 0cb6a87e..d5311595 100644 --- a/addon/components/field-for.js +++ b/addon/components/field-for.js @@ -526,7 +526,9 @@ export default class FieldForComponent extends Component { * @public */ @arg(bool) - required = false; + get required() { + return get(this, `form.model.validations.${this.for}.presence`) ?? false; + } /** * Whether or not this field is disabled diff --git a/addon/components/form-for.js b/addon/components/form-for.js index 2f3d0d50..e44451fc 100644 --- a/addon/components/form-for.js +++ b/addon/components/form-for.js @@ -501,6 +501,16 @@ export default class FormForComponent extends Component { @arg(bool) enforceRequiredFields = false; + @arg(bool) + get showRequiredIndicator() { + return this.formFor.showRequiredIndicator ?? false; + } + + @arg(string) + get requiredText() { + return this.formFor.requiredText; + } + /** * Resets the dirty model properties to their previous values on the form destruction. Turning this off will * leave the model in a dirty state, even when the user navigates away. diff --git a/addon/components/label-for.hbs b/addon/components/label-for.hbs index f1a82a51..2aef34a0 100644 --- a/addon/components/label-for.hbs +++ b/addon/components/label-for.hbs @@ -2,7 +2,7 @@ {{#if this.customLabelComponent}} {{component this.customLabelComponent for=@controlId label=@label}} {{else}} -