Angular 2+ reactive form validation - either this or that -
i have 3 field form - first name, last name, id number - want set user must either enter data 2 name fields or enter id number form valid , "submittable".
my code
ngoninit() { this.person = this.fb.group({ 'fname': [], 'lname': [], 'certid': [] },{ validator:(formgroup:formgroup)=>{ return this.validateinput(formgroup); } } ); } validateinput(formgroup:formgroup){ if(formgroup.controls["fname"].value && formgroup.controls["lname"].value || formgroup.controls["certid"].value){ console.log('ok'); return {validateinputdata:true}; } else { console.log('not ok'); return null; } }
while validation runs when form/page loaded , generates 'fail'('not ok'), submit button nevertheless active:
<button type="submit" class="btn btn-success" [disabled]="submitpending || person.invalid"> submit<span *ngif="submitpending" class="glyphicon glyphicon-refresh glyphicon-refresh-animate"></span> </button>
where going wrong?
are return statements backwards?
return null
when ok , error when not ok.
if((formgroup.controls["fname"].value && formgroup.controls["lname"].value) || formgroup.controls["certid"].value){ console.log('ok'); return null; } else { console.log('not ok'); return {validateinputdata: true}; }
the 'validateinputdata' property name of error in errors collection.
Comments
Post a Comment