Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<mat-radio-button> : No value accessor

I have this HTML:

 <mat-radio-group>
  <mat-radio-button  [(ngModel)]="searchType">
      And (Narrower search)
  </mat-radio-button>
  <mat-radio-button  [(ngModel)]="searchType">
       Or (Wider search)
  </mat-radio-button>
 </mat-radio-group>

I am getting this error:

Error: No value accessor for form control with unspecified name attribute

I tried using this instead:

 <mat-radio-button name="search-and"[(ngModel)]="searchType">
      And (Narrower search)
 </mat-radio-button>

and I get this error:

Error: No value accessor for form control with name: 'search-and'

so then I tried this:

 <mat-radio-button value="search-and" name="search-and" [(ngModel)]="searchType">
              And (Narrower search)
 </mat-radio-button>

and this:

  <mat-radio-button [value]="search-and" name="search-and" [(ngModel)]="searchType">
              And (Narrower search)
  </mat-radio-button>

and I still get this error:

Error: No value accessor for form control with name: 'search-and'

like image 672
Alexander Mills Avatar asked Dec 03 '25 09:12

Alexander Mills


2 Answers

Try to set formControlName="SearchType" on <mat-radio-group> and not on <mat-radio-button>.

like image 102
Aijaj Hussain Ansari Avatar answered Dec 04 '25 22:12

Aijaj Hussain Ansari


Ugghh my bad, ngModel belongs at the parent level:

  <mat-radio-group fxLayout="column" [(ngModel)]="searchType" (change)="onSearchTypeChange()">
     <mat-radio-button value="search-and" name="search-and">
          And (Narrower search)
     </mat-radio-button>
     <mat-radio-button  value="search-or" name="search-or">
         Or (Wider search)
    </mat-radio-button>
  </mat-radio-group>

Now everything works, no errors, not sure whether to use [value] or value though.

like image 31
Alexander Mills Avatar answered Dec 04 '25 23:12

Alexander Mills



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!