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

Update radio button directive to work with ReactiveForms #1023

Closed

Conversation

thecontrarycat
Copy link
Contributor

The current implementation of btnRadio only works if there is an [(ngModel)] binding on the element, and does not work correctly with ReactiveForms.

This PR removes the dependency upon ngModel while allowing the btnRadio directive to work in both a ReactiveForms (with formControlName) or TemplateForms (with [(ngModel)] environment. Furthermore, there is a change to the btnRadio to make it an object so that it can be correctly compared to the model object.

@Martin-Luft
Copy link
Contributor

Ahhhh, we need this for all components:

pagination.component
pager.component
datepicker.component
datepicker-popup.component
button-checkbox.directive
button-radio.directive
timepicker.component
rating.component

@thecontrarycat
Copy link
Contributor Author

Reactive Forms Example

<form [formGroup]="form">
    <div class="form-group">
        <label>Suitable for Vegetarians?</label>
        <div class="btn-group visible-xs-block visible-sm-block visible-md-block visible-lg-block clearfix">
            <label class="btn btn-primary" formControlName="isVegetarian" [value]="form.value.isVegetarian" [btnRadio]="false">No</label>
            <label class="btn btn-primary" formControlName="isVegetarian" [value]="form.value.isVegetarian" [btnRadio]="true">Yes</label>
        </div>
    </div>
</form>

@thecontrarycat
Copy link
Contributor Author

Template Forms Example

<form>
    <div class="form-group">
        <label>Suitable for Vegetarians?</label>
        <div class="btn-group visible-xs-block visible-sm-block visible-md-block visible-lg-block clearfix">
            <label class="btn btn-primary" [(ngModel)]="product.isVegetarian" [value]="product.isVegetarian" [btnRadio]="false">No</label>
            <label class="btn btn-primary" [(ngModel)]="product.isVegetarian" [value]="product.isVegetarian" [btnRadio]="true">Yes</label>
        </div>
    </div>
</form>

@thecontrarycat
Copy link
Contributor Author

Looks like the linter is preventing the use of forwardRef, but this seems to be necessary to get the ControlValueAccessor working correctly (see mgechev/angular2-style-guide#56 for other possible uses of forwardRef).

@valorkin valorkin closed this in 5d51939 Oct 4, 2016
@bjornharvold
Copy link

+1 one this one

@Martin-Luft
Copy link
Contributor

@bjornharvold this issue is closed and already fixed by 5d51939

jtomaszewski pushed a commit to jtomaszewski/ng2-bootstrap that referenced this pull request Oct 22, 2016
@RolfVeinoeSorensen
Copy link

Can someone show me a html markup and typescript example to use in ReactiveForms?
Is this fix allready in v1.1.16?

@roscopeeco
Copy link

Can someone show me a html markup and typescript example to use in ReactiveForms?
I am running v1.6.6 and cannot get button groups to work with reactive forms?

https://github.com/ng-bootstrap/ng-bootstrap/pull/1299/files

I have tried using markup from the above link but with no luck.

thanks

@pantonis
Copy link

pantonis commented May 9, 2017

@roscopeeco Having the same problem. Did you manage to make it work?

@roscopeeco
Copy link

No unfortunately not have ended up writing a custom input control

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants