Угловой 6 входной диапазон проверки - PullRequest
0 голосов
/ 04 июля 2019

Мне нужно проверить один из элементов ввода в моей ngForm

Как проверить диапазон при отправке этой формы Например, пользователь должен ввести цену от 1000 до 2000 долларов

Пожалуйста, дайте предложенияпродолжить

Спасибо

Ответы [ 2 ]

0 голосов
/ 05 июля 2019

Попробуйте этот пример работы

ссылка

<h3>Reactive Forms Validator Example</h3>
<br>

<label>We have created two validators using slightly different approaches that validate if the input is equal to "A" or not.</label>
<br><br>

<label>Doesn't work because the method is not executed which will return the custom validator function.</label><br>
<input type="text" [formControl]="wrongOnlyA"> 
<br><br>

<label>Works because the method is executed.</label><br>
<input type="text" [formControl]="correctOnlyA"> 
<br><br>

<label>Works because the validator function is directly exported.</label><br>
<input type="text" [formControl]="onlyB"> 

.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup } from "@angular/forms"

import { RxFormBuilder } from '@rxweb/reactive-form-validators';
import { FormBuilderConfiguration  } from '@rxweb/reactive-form-validators';

import { EmployeeInfo } from '../employee-info.model';

@Component({
    selector: 'app-employee-info-add',
    templateUrl: './employee-info-add.component.html'
})
export class EmployeeInfoAddComponent implements OnInit {

    employeeInfoFormGroup: FormGroup

    constructor(
        private formBuilder: RxFormBuilder
    ) { }

    ngOnInit() {
        let employeeInfo = new EmployeeInfo();
        let formBuilderConfiguration = new FormBuilderConfiguration();
        formBuilderConfiguration.validations = {
            age : {
                range :  {minimumNumber:18,maximumNumber:60,} 
            },
            experience : {
                range :  {minimumNumber:2,maximumNumber:20,conditionalExpression:'x => x.age >=25',} 
            },
            salary : {
                range :  {minimumNumber:1000,maximumNumber:200000,message:'Your Salary should be between 10000 to 200000.',} 
            },
        };
        this.employeeInfoFormGroup = this.formBuilder.formGroup(employeeInfo,formBuilderConfiguration);
    }
}
0 голосов
/ 05 июля 2019

Попробуйте это

<form [formGroup]="myForm">
<label for="priceRange">Price Range: </label>
 <input type="number" id="priceRange" formControlName="priceRange">
<div *ngIf="f.priceRange.errors">
 Invalid Price Range
</div>

в component.ts

import { FormBuilder, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';

   myForm = new FormGroup({});

   get f() { 
      return this.myForm.controls; 
    }

  constructor(private formBuilder: FormBuilder){
      this.myForm = formBuilder.group({     
     priceRange: ['', [Validators.min(1000), Validators.max(2000)]]
       });
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...