customvalidator 相关问题

.NET`CustomValidator`类,允许自定义验证任何UI输入元素

用于反应式表单的 Angular 自定义验证器将数组对象作为参数传递

我面临的情况是,将对象数组(产品列表)作为自定义验证器的参数传递。但它显示错误,例如“只能将单个值作为参数传递”。 我面临的情况是,将对象数组(产品列表)作为自定义验证器的参数传递。但它显示错误,例如“只能将单个值作为参数传递”。 <div class="formGroup"> <input formControlName="productname" matInput #input type="text" [matAutocomplete]="auto" class="form-control" (click)="clicked(input)" (input)="performFiltering(input)" /> <mat-autocomplete #auto="matAutocomplete" (optionSelected)="onProductChange1($event)" > <mat-option *ngFor="let product of filteroptions | async" [value]="product.productname" > {{product.productname}} </mat-option> </mat-autocomplete> <div *ngIf="productdetailsarray.at(i).get('productname')?.errors?.['productnotavailable'] && productdetailsarray.at(i).get('productname')?.touched"> <span style="font-size: 10pt; color:red">Product is not valid</span> </div> 我的ts代码是 export interface productentity{ productname:string, price:number, gst:number, } productlists = [ { productname: 'apple', price: 10, gst: 10 }, { productname: 'orange', price: 20, gst: 12 }, { productname: 'lemon', price: 30, gst: 20 }, { productname: 'grape', price: 60, gst: 20 }, { productname: 'bringal', price: 25, gst: 20 }, { productname: 'tomato', price: 40, gst: 20 }, { productname: 'kiwi', price: 120, gst: 20 }, { productname: 'potato', price: 500, gst: 20 }]; productlist!:productentity[]; filteroptions!: Observable<productentity[]>; productname: new FormControl('', [Validators.required,Productavailable(productlists)]), ngOnInit() { this.productlist = this.productlists.sort((a:any,b:any)=> { return a.productname < b.productname ? -1 : a.productname > b.productname ? 1 : 0 }); } clicked(input: any) { if (input.value) { this.performFiltering(input); } else{ this.filteroptions = of(this.productlist); } } performFiltering(input: any) { if (input.value) { this.filteroptions = of(this._filter(input.value)); } else { this.filteroptions = of(this.productlist); } } private _filter(value: string): productentity[] { const searchvalue = value.toLocaleLowerCase(); return this.productlist.filter((option) => option.productname.toLocaleLowerCase().includes(searchvalue) ); } 我的自定义验证器函数是 export function Productavailable(val: string): ValidatorFn { return (control: AbstractControl): ValidationErrors | null => { let productvalue: string = control.value; let productlists = [ { productname: 'apple', price: 10, gst: 10 }, { productname: 'orange', price: 20, gst: 12 }, { productname: 'lemon', price: 30, gst: 20 }, { productname: 'grape', price: 60, gst: 20 }, { productname: 'bringal', price: 25, gst: 20 }, { productname: 'tomato', price: 40, gst: 20 }, { productname: 'kiwi', price: 120, gst: 20 }, { productname: 'potato', price: 500, gst: 20 }]; const selectedProduct = productlists.find( (product) => product.productname === productvalue ); if (productvalue=='') { return null; } if (selectedProduct==null) { return { 'productnotavailable': true, 'requiredValue': val } } return null; } } 在这里,我面临着在组件 ts 和自定义验证器函数()中添加“productlists”数组对象的情况。因此,我无法动态提供产品列表。 有人可以帮助我如何将数组对象作为参数传递给自定义验证函数或任何其他解决方案以在产品列表中不可用时显示错误消息。 您可以在 Productavailable 上使用 bind 方法。 Function 实例的 bind() 方法创建一个新函数, 调用时,调用此函数并将其 this 关键字设置为 提供了价值。 注意:使用这种方法,验证器方法位于组件内部。 示例: productname: new FormControl('', [Validators.required, Productavailable.bind(this)]), ... function Productavailable(val: string): ValidatorFn { return (control: AbstractControl): ValidationErrors | null => { let productvalue: string = control.value; const selectedProduct = this.productlists.find( (product) => product.productname === productvalue ); if (productvalue=='') { return null; } if (selectedProduct==null) { return { 'productnotavailable': true, 'requiredValue': val } } return null; } }

回答 1 投票 0

Spring Boot 验证器不返回自定义错误消息

我想比较实体中的两个字段,并检查 maxField 是否大于 minField 才有效。下面是我的实现,但我看不到自定义消息,而是返回此 w...

回答 1 投票 0

“pydantic alidators.py”:找不到<class 'pandas.core.frame.DataFrame'>

以下 pandas 的 DataFrame 未经过 pydantic 验证。这要怎么处理呢? 从 pydantic.dataclasses 导入数据类 @数据类 类数据帧: dataframe1:pd.DataFrame = None 数据帧...

回答 4 投票 0

CustomValidator ServerValidate 方法不触发

我在表单上放置了一个 CustomValidator。我尚未设置其 ControlToValidate 属性。在其 ServerValidate 事件中我编写了以下内容: protected void CustomValidator1_ServerValidate(对象 ...

回答 3 投票 0

客户端验证后不会触发服务器验证

我在这里不知所措。我经常看到相同的问题答案,但没有一个能帮我解决问题。 我有一个接受电子邮件作为输入的文本框。如果用户输入有效的电子邮件,那么我想要...

回答 1 投票 0

使用自定义验证器验证@RequestBody的依赖属性的最简单方法

我得到了一个如下所示的端点: @PostMapping(产生= MediaType.APPLICATION_JSON_VALUE) 事件 createEvent (@RequestBody(required = true) @Valid EventRequestBody eventRequestBody) { …… } 我的

回答 1 投票 0

Spring Boot 自定义验证器未找到错误:HV000030

新手在学习如何使用自定义验证器(maven、h2 存储库、thymeleaf,不确定此信息是否隐含或必要,所以在这里)修改 Spring Boot 应用程序时遇到困难。我

回答 1 投票 0

获取“查询模板时出错:TypeError:无法读取未定义的属性(读取“findOne”)”

我正在尝试在 Nest js 应用程序的自定义验证器类中使用我的 MongoDB 模型之一。我的模型是在名为“model”的文件夹中定义的。我正在使用 InjectModel 猫鼬 t...

回答 1 投票 0

当错误的数据类型作为输入提交时,Flask wtforms DataRequired 验证器触发(而不是自定义或默认验证器)

尝试将 DataRequired() 和自定义验证程序 validate_someNumber 附加到 FloatField。但是当验证错误时,我很难理解表单字段错误的反馈......

回答 1 投票 0

Class-Validator node.js提供自定义的错误信息

我有一个自定义的验证器约束和注解,用于检查给定属性的实体是否已经存在,以下是代码 import { Inject, Injectable } from '@nestjscommon';...。

回答 1 投票 0

jQuery验证数据属性中的自定义规则

我有一个已经有一些验证规则的输入文本

回答 1 投票 5

Spring:不使用休眠的唯一ID验证程序

在执行POST时,通过验证程序检查是否没有重复ID的最佳方法是什么?我试图创建一个自定义验证器,但始终收到内部服务器错误{“ timestamp”:“ 2020 -...

回答 1 投票 0

为什么我的AsyncValidator总是返回true?

为什么这总是返回错误?导出类UserExistsValidatorService实现AsyncValidator {构造函数(private userService:UserService){} validate(control:FormControl):...

回答 1 投票 1

如何在ASP.NET Core中创建也触发无效输入的自定义验证器?

我已经为ASP.NET Core 3.1中的DateTime字段创建了一个自定义验证器,如下所示:[CustomDate] public DateTime DOB {get;组; }公共类CustomDate:ValidationAttribute {...

回答 1 投票 0

Angular 9-无法读取未定义的属性

我正在使用Angular CLI 9.0.7运行应用程序。在此应用程序中,存在一个带有字段的表格,需要对其控制数字进行验证。为此,我创建了一个自定义验证器。当...

回答 2 投票 0

JQuery自定义验证尽管正确,仍无法正常工作?

[我很困惑,因为我添加了正确的库,我的NON自定义验证工作正常,我在所有标签上都有名称,拼写正确吗?((((我什至尝试了一个...

回答 1 投票 0

Angular反应形式:从控件中删除特定的验证器

我正在使用Angular反应形式。在我的方案中,验证器是动态添加到我的控件中的。如下所示:const myControl = myFormGroup.get('myControl');如果(myControl ....

回答 1 投票 0

是否可以修改模式验证器以包括删除输入字段中写入的空格?

我正在运行一个验证器,在该验证器中,我检查输入的电话格式是否与后端请求的信息兼容。是否可以修改此正则表达式,以便删除...

回答 1 投票 0

您是否必须在symfony中注册您的自定义验证器

我正在阅读有关制作自定义验证器的文档,它没有说明您必须注册自定义创建的验证器,但是还有什么会导致此问题?我创建了一个自定义...

回答 1 投票 2


© www.soinside.com 2019 - 2024. All rights reserved.