Reactive form-validation-angular2

你。 提交于 2020-01-06 23:51:13

问题


I am able to get the values from the child but I need to validate the values in my parent component that is present or not.

parent.html :

<browser (notify)="onselect($event)"  [template]="coreActivity" (onselect)="onselect($event)" formControlName="template" ></browser>

parent.ts :

export class CreateCommunityComponent implements OnInit {  userForm: FormGroup;  public tagarray=[];   coreActivity = [
   {value: 'Professional', viewValue: 'Professional', tool: 'Forum' },
   {value: 'Travel', viewValue: 'Travel', tool: 'quora'},
   {value: 'Arts', viewValue: 'Arts', tool: 'stackoverflow'},
   {value: 'Technology', viewValue: 'Technology', tool: 'quora'},
   {value: 'Business', viewValue: 'Business', tool: 'Forum'},
   {value: 'Science', viewValue: 'Science', tool: 'quora'},
   {value: 'Education', viewValue: 'Education', tool: 'quora'}
 ];    visibility = [
     {value: 'Public', viewValue: 'Public'},
     {value: 'Private', viewValue: 'Private'},
     {value: 'Moderate', viewValue: 'Moderate'}
   ];    tags = [
     {value: 'tag-one', viewValue: 'tagone'},
     {value: 'tag-two', viewValue: 'tagtwo' },
     {value: 'tag-three', viewValue: 'tagthree'}
   ];
 constructor(private dialog: MdDialog, private fb: FormBuilder,private router: Router) {    this.createForm();    }  createForm() {
       this.userForm = this.fb.group({
         domainName: ['', [Validators.required, Validators.pattern('[a-z.]{8,20}')]],
         communityName: ['', Validators.required],
         Purpose: ['', Validators.required],
         visibility: ['Public', Validators.required],
         template: ['',Validators.required],
         tagSelection: ['', Validators.required],
         termscondition: ['', Validators.required]
       });
   }
//  check whether the card is clickable or not 
onselect(selectedTemplate: any)
{
  console.log(selectedTemplate);
  return selectedTemplate;
}

It is giving error while I am validating the child value is present or not:

ERROR Error: No value accessor for form control with name: 'template'


回答1:


You need to add ngDefaultControl to your <browser> tag.

<browser ngDefaultControl (notify)="onselect($event)"  
  [template]="coreActivity" (onselect)="onselect($event)" 
formControlName="template" ></browser>

Hope this will work for you.



来源:https://stackoverflow.com/questions/44645272/reactive-form-validation-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!