Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 not validating conditional [required]

Tags:

angular

I have the following field in an NgForm:

<input type="text" name="depVarName" [(ngModel)]="variable.depVarName" 
       #depVarName="ngModel" id="depVarName"  
       [required]="'depSelected>0'" />
<div *ngIf="submitted" class="errorMsg">
   <label *ngIf="depVarName.control.hasError('required')">Dependency is required</label>
</div>

The problem is that this field is always required, regardless of the [required] related condition depSelected>0. depSelected is not a DOM element, could that be the problem?

like image 495
ps0604 Avatar asked Oct 23 '25 12:10

ps0604


1 Answers

Currently 'depSelected>0'(non-empty string) is considered as truthy and that's why field marked as required. The expression inside require attribute should be without '(single qoute).

[required]="depSelected>0" 
like image 162
Pankaj Parkar Avatar answered Oct 26 '25 02:10

Pankaj Parkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!