Skip to content

Update FormBuilder#control() to support AbstractControlOptions as 2nd parameter #19163

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

Closed
Klinton90 opened this issue Sep 13, 2017 · 24 comments
Closed

Comments

@Klinton90
Copy link

Klinton90 commented Sep 13, 2017

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x ] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request 

Current behavior

In 5.x version AbstractControlOptions has been added as parameter to FormControl constructor. FormBuilder#control() method should create new control by provided configuration. Currently signature/input parameters do not match FormControl.

Expected behavior

control(
     formState: any = null,
      validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null,
      asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null
): FormControl {
    return new FormControl(formState, validatorOrOpts, asyncValidator);
  }

Minimal reproduction of the problem with instructions

this.fb.group({
    "input1": this.fb.control('', {updateOn: 'submit'})
});

What is the motivation / use case for changing the behavior?

Just to keep code consistent

Environment

Angular version: 5.0.0-beta6

Other

It would be nice, if you could also update FormBuilder#group() to support AbstractControlOptions when simple json is provided.

@artuska
Copy link

artuska commented Dec 26, 2017

I also have this issue.

let someObject = {
  foo: 'bar'
};

let group = this.formBuilder.group(someObject, {updateOn: 'blur'});

Here the FormBuilder just ignores passed options.

@vivainio
Copy link

vivainio commented Jan 8, 2018

What's the recommended workaround? Not to use FormBuilder if you need these?

@michael-letcher
Copy link

I've also noted that in the Form Builder - Group docs it mentions

Valid keys for the extra parameter map are validator and asyncValidator.

But in the FormControl docs it show that you can add the updateOn.

I assume this was missed when updateOn was added.

@michael-letcher
Copy link

michael-letcher commented Jan 10, 2018

@vivainio

WORKAROUND
This works for me for now:

const controlConfig: {} = {};

// Loop through all fields and set default values and if field is required
fields.forEach(field => {
     controlConfig[field.key] = [field.value || '', { updateOn: 'blur', validators: field.validators || [] }];
});

return this.fb.group(controlConfig);

What should work though, but doesn't currently:

const controlConfig: {} = {};

// Loop through all fields and set default values and if field is required
fields.forEach(field => {
  controlConfig[field.key] = [field.value || '', field.validators || []];
});

return this.fb.group(controlConfig, { updateOn: 'blur' });

@dcesiel
Copy link

dcesiel commented Jan 17, 2018

I had no luck with the workaround. I will wait for this to be resolved before using updateOn.

@ngbot ngbot bot added this to the Backlog milestone Jan 23, 2018
@joaopslins
Copy link

+1 for this feature. It's very inconvenient that we need to use FormControl constructors instead of FormBuilder to set validation onBlur.

@Buttonsz
Copy link

+1 for this for consistency and ease of use.

@dthomas722
Copy link

+1 for this for consistency and ease of use as well.

For others looking for a work around until this is resolved, the code below works for us:

let myFormGroup = this.fb.group({
  control1: 'control 1 value',
  control2: 'control 2 value'
});

myFormGroup = new FormGroup(formGroupWithoutBlur.controls, { updateOn: 'blur' });

@vytautas-pranskunas-
Copy link

Same here

@gassmannT
Copy link

    1. Same here

@ngbot ngbot bot modified the milestones: Backlog, needsTriage Feb 26, 2018
@GaryBirges
Copy link

GaryBirges commented Mar 26, 2018

Is it the same issue if I want to have 2 validators like this:

this.newTransactionForm = this.fb.group({
 amount:              ['10', {Validators: [Validators.required,  Validators.min(1)],  updateOn: 'blur' }]
})

@fralewsmi
Copy link

Would love to see this feature

@LayZeeDK
Copy link
Contributor

LayZeeDK commented Jun 13, 2018

Thank you, @dthomas722!

Just to clear up your snippet

let myFormGroup = this.formBuilder.group({
  control1: 'control 1 value',
  control2: 'control 2 value',
});
myFormGroup = new FormGroup(myFormGroup.controls, { updateOn: 'blur' });

or

const myFormGroup = new FormGroup(this.formBuilder.group({
  control1: 'control 1 value',
  control2: 'control 2 value',
}).controls, { updateOn: 'blur' });

@cezarcruz
Copy link

will be great!!

@pkaushik23
Copy link

@kara just to add more information to the bug, the solution as suggested by @LayZeeDK & @dthomas722 works when we use a default html control like input but the solution does not work when we have a custom form control being used. example below:

<my-text-edit [formControlName]="controlInfo.name" ></my-text-edit>

For some one looking for a workaround (Please correct me if my usage is incorrect for custom form control)

The work around is to not to have the [formControlName] on the template where custom control is called as shown above, but to have it inside the template of myTextEditComponent. Although this does not look nice because now i have to pass in the value by exposing another @input property which takes the value for the control. for example

template for myTextEditComponent

<input type="text" style="border: solid blue" [formControlName]="controlName" />

And the first code snippet changing to

<my-text-edit [controlName]="controlInfoProp.name" [controlData] = "controlInfoProp.data" >
</my-text-edit>

UPDATE
if we dont specify the [formControlName] on the usage of <my-text-edit>, then methods of controlValueAccessor are not called. There i ultimately ended up doing following

while calling <my-text-edit>

<div [formGroup]="formGroupRef">
    <my-text-edit [controlName]="controlInfoProp.name" [formGroupRef]="formGroupRef" [formControlName]="controlInfoProp.name" ></my-text-edit>
</div>

Template of myTextEditComponent

<div [formGroup]="formGroupRef">
    <input type="text" style="border: solid blue" [formControlName]="controlName" (input)="OnValChange($event.target.value)"/>
</div>

Thanks,

@AhHa45
Copy link

AhHa45 commented Oct 8, 2018

what's the status on this?

@LayZeeDK
Copy link
Contributor

LayZeeDK commented Oct 12, 2018

LGTM for api but let's wait until we branch off 7.0.x before we merge this.
@IgorMinar in #24599

It is coming! 🙌 (version 7.1.0 at the earliest)

AbstractControlOptions (updateOn and others) will be supported in:

  • FormBuilder#array
  • FormBuilder#control
  • FormBuilder#group

@LayZeeDK
Copy link
Contributor

LayZeeDK commented Nov 2, 2018

PR #24599 code has been merged. updateOn and the other AbstractControlOptions will be supported by FormBuilder in Angular 7.1.

@LayZeeDK
Copy link
Contributor

LayZeeDK commented Nov 2, 2018

@pkaushik23: @kara delivered the best talk on Angular Forms at AngularConnect 2017 where she shows how to avoid the anti-pattern of passing form groups down the component tree by injecting ControlContainer in the constructor of your custom control component.

@LayZeeDK
Copy link
Contributor

LayZeeDK commented Nov 7, 2018

You can now try out this feature in @angular/forms@7.1.0-beta.2.

@LayZeeDK
Copy link
Contributor

LayZeeDK commented Nov 7, 2018

@LayZeeDK
Copy link
Contributor

#24599 was released with @angular/forms@7.1.0 🎉

@kara You can close this issue.

@kara
Copy link
Contributor

kara commented Nov 22, 2018

@LayZeeDK Thanks for your help getting this fixed! Closing as done.

@kara kara closed this as completed Nov 22, 2018
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests