forms - Cannot pass text argument to validator Angular2 -
i'm trying validate wether username taken backend. had issues calling validator (link here custom validator angular 2) whenever attempt check if username taken errors. here code:
form.component.ts
import { component } '@angular/core'; import { ngform, validators, validatorfn, validationerrors } '@angular/forms'; import { injectable } '@angular/core'; import { http, headers, response, requestoptions } '@angular/http'; import { formbuilder, formgroup, formcontrol, abstractcontrol } '@angular/forms'; import { observable } 'rxjs/observable'; import 'rxjs/add/operator/map'; interface validator<t extends formcontrol> { (c: t): { [error: string]: }; } function validateusername() : validationerrors { return (this.checkuser(this.complexform['username'].value)=='y') ? null : { validateusername: { valid: false } }; } @component({ selector: 'form1', templateurl: './form1.html', moduleid: module.id, }) export class form1component { public roles = [ { value: '1', display: '1' }, { value: '2', display: '2' }, { value: '3', display: '3' } ]; private complexform: formgroup; constructor(private http: http, fb: formbuilder) { this.complexform = fb.group({ 'username': [null, [ validators.required, validators.minlength(5), validators.maxlength(10), ], [validateusername]], 'password': '', 'group_id': '0', 'firstname': '', 'lastname': '', 'organization': '', 'title': '', 'email': '', 'phone': '', }) } public checkuser(username: string) { let url = '/check user url/' + username; let usernameavailable; let headers = new headers(); headers.append('user', sessionstorage.getitem('username')); headers.append('token', sessionstorage.getitem('token')); headers.append('accesstime', sessionstorage.getitem('accesstime')); let options = new requestoptions({ headers: headers }); this.http.get(url, options) .subscribe((res: response) => { usernameavailable = res.json()} ); console.log(usernameavailable); return usernameavailable; } } html component username
<div class="form-group" [ngclass]="{'has-error':!complexform.controls['username'].valid}"> <label>username</label> <input class="form-control" id="username" type="text" [validator] <-- // custom validator="complexform.controls['username']" [formcontrol]="complexform.controls['username']"> <div *ngif="complexform.controls['username'].haserror('required') && complexform.controls['username'].touched" class="alert alert-danger">you must include username.</div> <div *ngif="complexform.controls['username'].haserror('minlength') && complexform.controls['username'].touched" class="alert alert-danger">your username must @ least 5 characters long.</div> <div *ngif="complexform.controls['username'].haserror('maxlength') && complexform.controls['username'].touched" class="alert alert-danger">your username cannot exceed 10 characters.</div> </div> my custom validator failing pass textfield arguement function check if username taken or not.
this error i'm getting whenever start typing textfield:
error typeerror: cannot read property 'checkuser' of undefined @ validateusername (form1.component.ts:15) @ forms.es5.js:437 @ array.map (<anonymous>) @ _executeasyncvalidators (forms.es5.js:437) @ forms.es5.js:399 @ forms.es5.js:437 @ array.map (<anonymous>) @ _executeasyncvalidators (forms.es5.js:437) @ formcontrol.asyncvalidator (forms.es5.js:399) @ formcontrol.abstractcontrol._runasyncvalidator (forms.es5.js:2632) @ formcontrol.abstractcontrol.updatevalueandvalidity (forms.es5.js:2593) @ formcontrol.setvalue (forms.es5.js:2950) @ defaultvalueaccessor.onchange (forms.es5.js:1753) @ defaultvalueaccessor._handleinput (forms.es5.js:621) @ object.eval [as handleevent] (form1component.html:7) i can provide more details if needed.
thank you!
Comments
Post a Comment