18

There is a custom input component and it is used in a reactive form with validation:

@Component({
    moduleId: module.id.toString(),
    selector: 'custom-select',
    templateUrl: 'custom-select.component.html',
    styleUrls: ['custom-select.component.css']
})
export class CustomSelectComponent {
    @Input() public items: SelectModel[];
    public model: SelectModel;
    constructor(private customSelectService: CustomSelectService) {
        this.customSelectService.Selected.subscribe((data: SelectModel) => {
            this.model = data;
        });
    }
    public newSelect(select: SelectModel): void {
        this.customSelectService.updateSelected(select);
    }
}

which works fine, I am using custom-select in a reactive form and want to validate it like below:

<custom-select id="country" [items]="selectItems" formControlName="country"></custom-select>
<div *ngIf=" myFrom.controls['country'].invalid && (myFrom.controls['country'].dirty 
             || myFrom.controls['country'].touched) " class="ha-control-alert">
    <div *ngIf="myFrom.controls['country'].hasError('required')">Country is required</div>
</div>

this is how I declare the form in component

this.myFrom = this.formBuilder.group({
    country: [null, Validators.required],
})

but when I add formControlName for validations, it gets error which says No value accessor for form control with name: 'country'. How should I handle this?

7
  • value accessor need to be implemented in custom inputs. can you post custom-select Commented Sep 10, 2017 at 14:23
  • 5
    You need to implement ControlValueAccessor, then it will work with both kinds of form. @Rajez that's already in the post Commented Sep 10, 2017 at 14:25
  • @Rajez I've already posted, bad formatting maybe Commented Sep 10, 2017 at 14:26
  • 2
    Please read the documentation I already linked to, which tells you which methods to implement and provides example implementations. Commented Sep 10, 2017 at 14:28
  • 8
    blog.thoughtram.io/angular/2016/07/27/… it shows how to create custom form controls Commented Sep 10, 2017 at 14:31

2 Answers 2

8

STEPS

  1. Add the provider for NG_VALUE_ACCESSOR in the decorator
  2. Implement the ControlValueAccessor in the class
  3. Create the onChange function like this onChange = (value: boolean) => {};
  4. Add the registerOnChange, writeValue and registerOnTouched methods of the ControlValueAccessor interface
  5. In the method that will be changed the value of select, call the onChange function passing as parameter the value of select.
        import ...
        import { Component, forwardRef } from '@angular/core';
        import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
        
    
        @Component({
            moduleId: module.id.toString(),
            selector: 'custom-select',
            templateUrl: 'custom-select.component.html',
            styleUrls: ['custom-select.component.css'],
            // STEP 1
            providers: [{
              provide: NG_VALUE_ACCESSOR,
              multi: true,
              useExisting: forwardRef(() => CustomSelectComponent)
            }]
        })
        // STEP 2
        export class CustomSelectComponent implements ControlValueAccessor {
            // STEP 3
            onChange = (value: SelectModel) => {};
            @Input() public items: SelectModel[];
            public model: SelectModel;
            constructor(private customSelectService: CustomSelectService) {
                this.customSelectService.Selected.subscribe((data: SelectModel) => {
                    this.model = data;
                });
            }
            public newSelect(select: SelectModel): void {
                // STEP 5
                this.onChange(select);
                this.customSelectService.updateSelected(select);
            }
            // STEP 4
            registerOnChange(fn: (value: SelectModel) => void): void {
                this.onChange = fn;
            }
            writeValue() {}
            registerOnTouched(){}
        }

Don't forget to add the formControlName in the selector.

Sign up to request clarification or add additional context in comments.

Comments

2

Add this to the providers of the child:

viewProviders: [{
  provide: ControlContainer, 
  useExisting: FormGroupDirective 
}]

The reason it fails in the first place is it doesn't look in the parent scope for FormGroup, so this just passes it down to the child.

Then you need to trick it into thinking your control has a valueAccessor - which it doesn't need because you just created a sort of 'shim'.

export const NOOP_VALUE_ACCESSOR: ControlValueAccessor = 
{
    writeValue(): void {},
    registerOnChange(): void {},
    registerOnTouched(): void {}
};

Then in the constructor for your child:

constructor(@Self() @Optional() public ngControl: NgControl) 
{
    if (this.ngControl)
    {
        this.ngControl.valueAccessor = NOOP_VALUE_ACCESSOR;
    }
}

Then you can use the formControlName as normal inside the child.

If you need to pass the formControlName from the parent component (which you probably would if it was meant to be a reusable control) just create a string @Input property and do this in the child:

    <input matInput [placeholder]="desc" 
           [formControlName]="formControlName">

5 Comments

Thanks to stackoverflow.com/a/65177817/16940 for the NOOP portion of this answer.
Hi @Simon_Weaver. Does this code work if "matInput" is put inside a nested formGroup of the main form? I tried this code and it doesn't seems to work in that case
@Simon_Weaver is there any way to get this to work with [(ngModel]) bindings?
@Hafnernuss I actually very rarely use those bindings so I can't immediately say. Hope you figured something out :-)
@Matt found a workaround. I had the same issue. Instead of passing the formControlName, you have to pass formControl. Definitely not so convenient, but it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.