angularjs - angular2 validating multiple controls in one validator -
i validate loan calculation form depending on choices of end user.
for example , if user clicks red button(that specific loan type) second text input should 36 max.
therefore access values of more 1 control during validation.
to achieve ve declared ngmodelgroup inside form , custom validator class.
the problem when validator hit can not access values of user controls inside validate method.
thanks in advance.
import { directive, forwardref, input } '@angular/core'; import { abstractcontrol, ng_validators, validator } '@angular/forms'; @directive({ selector: '[kredivadesayisi-validator]', providers: [ { provide: ng_validators, multi: true, useexisting: forwardref(() => kredivadesayisivalidator) } ] }) export class kredivadesayisivalidator implements validator { @input('kredivadesayisi-validator') msg = 'hata mesaj'; validate(c: abstractcontrol): { [key: string]: } { console.log('console hit'); // displayed on console return (group) => { console.log('valid'); if (group) { //always skipped (let ctrl in group.controls) { console.log('group'+group.controls[ctrl].value); } } return null; } } }
<form (ngsubmit)="f.form.valid&&kredihesaplasubmitted()" #f="ngform" novalidate> <div class="table-responsive "> <table class="table table-striped"> <thead> <tr class="info"> <th colspan="2" class="text-center">yeni kredi hesaplama </th> </tr> </thead> <tr class="default"> <td>kredi türü seçiniz</td> <td> <div class="btn-group"> <div #kredictrl="ngmodelgroup" ngmodelgroup="kredictrl" kredivadesayisi-validator> <label class="btn btn-default btn-sm" [(ngmodel)]="kredihesaplamamodel.radiomodelkreditur" name="kredihesaplamamodel.radiomodelkreditur" btnradio="0" (click)="onradiobuttonkredihesaplamaturchange()">İhtiyaç kredisi</label> <label class="btn btn-danger btn-sm" [(ngmodel)]="kredihesaplamamodel.radiomodelkreditur" name="kredihesaplamamodel.radiomodelkreditur" btnradio="4" (click)="onradiobuttonkredihesaplamaturchange()">aidat karşılığı kredi</label> <label class="btn btn-primary btn-sm" [(ngmodel)]="kredihesaplamamodel.radiomodelkreditur" name="kredihesaplamamodel.radiomodelkreditur" btnradio="1" (click)="onradiobuttonkredihesaplamaturchange()">konut kredisi</label> <label class="btn btn-default btn-sm" [(ngmodel)]="kredihesaplamamodel.radiomodelkreditur" name="kredihesaplamamodel.radiomodelkreditur" btnradio="2" (click)="onradiobuttonkredihesaplamaturchange()">taşıt kredisi</label> </div> </div> </td> </tr> <tr class="default"> <td>vade sayısı</td> <td> <div [ngclass]="{'has-error':f.submitted && !kreditutar.valid}"> <input type="number" class="form-control" name="kredivadesayisi" [(ngmodel)]="kredihesaplamamodel.kredivadesayisi" #kredivadesayisi="ngmodel" required maxlength="2" kredivadesayisi-validator/> <div *ngif="f.submitted && !kredivadesayisi.valid" class="help-block">vade bilgisi zorunludur.aidat karşılığı için en fazla 36 </div> </div> </td> </tr> </div> </form>
Comments
Post a Comment