Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

directive: add support for nested groups #21

Open
carlo-nomes opened this issue Apr 24, 2019 · 3 comments
Open

directive: add support for nested groups #21

carlo-nomes opened this issue Apr 24, 2019 · 3 comments
Labels
comp: directive enhancement New feature or request
Milestone

Comments

@carlo-nomes
Copy link
Contributor

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/NationalBankBelgium/ngx-form-errors/blob/master/CONTRIBUTING.md#got-a-question-or-problem

Currently messages are stored as {group?}.{formControlName?}.{error} meaning an error can only be specified for it's nearest group. I propose we allow nesting groups so the format will be {[group]?}.{formControlName?}.{error}.

@SuperITMan
Copy link
Member

SuperITMan commented Apr 26, 2019

Could you please provide some examples of what you have in mind ? 😊

@carlo-nomes
Copy link
Contributor Author

Yes, sorry I had some difficulty explaining. 😅
Lets say you have a form like this:

<form>
  <div ngxFormErrorsGroup="register-group">
    <input formControlName="userName" />
    <div ngxFormErrorsGroup="password-group">
      <input formControlName="password" />
      <input formControlName="repeatPassword" />
    </div>
  </div>
</form>

and your config:

const errorMessages = {
  required: "This is required",
  "register-group": {
    required: "Field is required for registering",
    "password-group": {
      passwordRepeat: {
        required: "Please validate your password"
      }
    }
  }
};

The error message for register-group.password-group.password will fall back to "Field is required for registering" in stead of "This is required".

@christophercr christophercr added comp: directive enhancement New feature or request labels Jun 14, 2019
@christophercr christophercr changed the title Nested Groups directive: add support for nested groups Jun 14, 2019
@christophercr christophercr added this to the 1.0.0-beta.2 milestone Jun 14, 2019
@christophercr christophercr modified the milestones: 1.0.0-rc.0, 1.0.0 Oct 30, 2019
@christophercr christophercr modified the milestones: 1.0.0, 1.1.0 Nov 29, 2019
@bernardthibaut
Copy link

bernardthibaut commented Apr 7, 2022

To support nested groups I don't think we need to add anything to the logic, but changing the type of the directive in the constructor would make it possible.

Changing _form: FormGroupDirective to _form: ControlContainer in the form-errors.directive.ts file makes the component use the nested formGroup instead of the main group. For example, there is a main group called formGroup, containing another formGroup address which contains a FormControl street. With FormGroupDirective it throws an error but with ControlContainer it correctly uses the address formGroup instead of the main formGroup.

<form [formGroup]="formGroup">
  <div formGroupName="address">
	<mat-form-field>
		<input matInput type="text" formControlName="street" />
		<mat-error>
			<ng-template ngxFormErrors="street"></ng-template>
		</mat-error>
	</mat-form-field>
  </div>
</form>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp: directive enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants