GVKun编程网logo

NestJS类验证器不验证数据(struts2验证器)

21

如果您想了解NestJS类验证器不验证数据的相关知识,那么本文是一篇不可错过的文章,我们将对struts2验证器进行全面详尽的解释,并且为您提供关于Angular2表单验证,minLength验证器不

如果您想了解NestJS类验证器不验证数据的相关知识,那么本文是一篇不可错过的文章,我们将对struts2验证器进行全面详尽的解释,并且为您提供关于Angular 2表单验证,minLength验证器不起作用、asp.net – 正则表达式验证器不验证空文本框、asp.net-mvc – 自定义模型绑定器不验证模型、Babel 在 NX 库中使用类验证器失败的有价值的信息。

本文目录一览:

NestJS类验证器不验证数据(struts2验证器)

NestJS类验证器不验证数据(struts2验证器)

您是在自己构造对象,因此除非您手动调用它,否则不会进行验证。必须在操​​作的输入参数上使用类型,以便框架可以构造并验证它。

类似这样:

async addEducationDetails(
    @Body() profileData: UserEducationDto,// <- this
    @GetUser() userId: any,)

Angular 2表单验证,minLength验证器不起作用

Angular 2表单验证,minLength验证器不起作用

我有以下Angular 2形式:
<register>
    <form [ngFormModel] = "registrationForm">
        <div>
            <labelfor="email">Email</label>
            <inputtype="email" id="email" ngControl="email" #email="ngForm">
        </div>
        <div *ngIf = "email.touched && email.errors">
            <div *ngIf = "!email.errors.required && email.errors.underscoreNotFound">
                <span>Underscore is required</span> 
            </div>
            <div *ngIf = "email.errors.required">
                <span>Email is required</span>
            </div>
        </div>
        <div>
            <labelfor="password">Password</label>
            <inputtype="password" id="password" ngControl="password" #password="ngForm">
        </div>
        <div *ngIf = "password.touched && password.errors">
            <div *ngIf = "password.errors.minLength && !password.errors.required">
                <span>Password should contain 6 characters</span>
            </div>  
            <div *ngIf = "password.errors.required">
                <span>Password is required</span>
            </div>          
        </div>
    </form>
</register>

这是我实现验证器的组件:

import {Component} from '@angular/core';
import {Control,ControlGroup,FormBuilder,Validators} from '@angular/common';
import {CustomValidator} from './CustomValidator';

@Component({
    selector: 'register',templateUrl: './app/authentication/register_validation/register.html',})

export class RegisterComponent{
    registrationForm: ControlGroup;

    constructor(formBuilder:FormBuilder)
    {
        this.registrationForm = formBuilder.group({
            email: ['',Validators.compose([Validators.required,CustomValidator.underscore])],password: ['',Validators.minLength(6)])]
        });
    }

}

在这种形式下,电子邮件字段对两个验证器都正常工作,即当我不输入任何内容时,它会给出“需要电子邮件”的消息,当我开始输入内容时,它会给出“需要下划线”的消息,当我输入“_”时所有错误消息都消失了.但是,当我尝试在密码字段上应用这样的2个验证器时,它不起作用.当我不输入密码时,它会显示“需要密码”的消息.但是当我键入少于6个字符的内容时,minLength消息根本不会出现.这段代码有什么问题?

error key is minlength而不是minLength:
<div *ngIf = "password.hasError('minlength') && !password.hasError('required')">
  <span>Password should contain 6 characters</span>
</div>

asp.net – 正则表达式验证器不验证空文本框

asp.net – 正则表达式验证器不验证空文本框

我想验证特定文本的文本框,它不能为空.但是正则表达式验证器不验证文本框是否为BLANK.但是,它验证我是否在文本框中键入内容.

即使文本框为空,如何使正则表达式触发?

我应该同时使用required Validator Regex Validator吗?谢谢.

<asp:TextBox ID="txtcard" runat="server" MaxLength="16"></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" 
ControlTovalidate="txtcard" ErrorMessage="Please type credit card no" 
ValidationExpression="^\d{16}$"></asp:RegularExpressionValidator>

解决方法

我通常会按照你的建议做,并有一个必要的验证器.这将允许您为每个规则添加不同的消息.

我建议任何Web开发人员看的另一个选项是JQuery验证插件.如果将此与Fluent验证结合使用,则可以将业务对象的所有验证规则保存在一个位置,并且可以使用相同的规则在客户端和服务器上进行验证.

JQuery Validation

Fluent Validation

asp.net-mvc – 自定义模型绑定器不验证模型

asp.net-mvc – 自定义模型绑定器不验证模型

我开始玩knockout.js,在这样做的时候我使用了FromJsonAttribute(由Steve Sanderson创建).我遇到一个问题,自定义属性不执行模型验证.我把一个简单的例子放在一起 – 我知道它看起来像很多代码 – 但是基本的问题是如何强制在自定义模型绑定器中验证模型.
using System.ComponentModel.DataAnnotations;

namespace BindingExamples.Models
{
    public class Widget
    {
        [required]
        public string Name { get; set; }
    }
}

这里是我的控制器:

using System;
using System.Web.Mvc;
using BindingExamples.Models;

namespace BindingExamples.Controllers
{
    public class WidgetController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(Widget w)
        {
            if(this.ModelState.IsValid)
            {
                TempData["message"] = String.Format("Thanks for inserting {0}",w.Name);
                return RedirectToAction("Confirmation");
            }
            return View(w);
        }

        [HttpPost]
        public ActionResult PostJson([koListEditor.FromJson] Widget w)
        {
            //the ModelState.IsValid even though the widget has an empty Name
            if (this.ModelState.IsValid)
            {
                TempData["message"] = String.Format("Thanks for inserting {0}",w.Name);
                return RedirectToAction("Confirmation");
            }
            return View(w);
        }

        public ActionResult Confirmation()
        {
            return View();
        }

    }
}

我的问题是模型总是在我的PostJson方法中有效.为了完整性,这里是FromJson属性的Sanderson代码:

using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace koListEditor
{
    public class FromJsonAttribute : CustomModelBinderAttribute
    {
        private readonly static JavaScriptSerializer serializer = new JavaScriptSerializer();

        public override IModelBinder GetBinder()
        {
            return new JsonModelBinder();
        }

        private class JsonModelBinder : IModelBinder
        {
            public object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)
            {
                var stringified = controllerContext.HttpContext.Request[bindingContext.ModelName];
                if (string.IsNullOrEmpty(stringified))
                    return null;
                var model = serializer.Deserialize(stringified,bindingContext.ModelType);
                return model;
            }
        }
    }
}

解决方法

描述

FromJsonAttribute只绑定到模型,如你所说,没有验证.

您可以向FromJsonAttribute添加验证,以便根据其DataAnnotations属性验证模型.

这可以使用TypeDescriptor类完成.

TypeDescriptor Provides information about the characteristics for a component,such as its attributes,properties,and events.

查看我的解决方案我已经测试了

private class JsonModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)
    {
        var stringified = controllerContext.HttpContext.Request[bindingContext.ModelName];
        if (string.IsNullOrEmpty(stringified))
            return null;
        var model = serializer.Deserialize(stringified,bindingContext.ModelType);

        // DataAnnotation Validation
        var validationResult = from prop in TypeDescriptor.GetProperties(model).Cast<PropertyDescriptor>()
                                from attribute in prop.Attributes.OfType<ValidationAttribute>()
                                where !attribute.IsValid(prop.GetValue(model))
                                select new { Propertie = prop.Name,ErrorMessage = attribute.FormatErrorMessage(string.Empty) };

        // Add the ValidationResult's to the ModelState
        foreach (var validationResultItem in validationResult)
            bindingContext.ModelState.AddModelError(validationResultItem.Propertie,validationResultItem.ErrorMessage);

        return model;
    }
}

更多信息

> TypeDescriptor Class
> System.ComponentModel.DataAnnotations Namespace

Babel 在 NX 库中使用类验证器失败

Babel 在 NX 库中使用类验证器失败

如何解决Babel 在 NX 库中使用类验证器失败?

我有一个 NX Typescript 库,它引入了“类验证器”。

我遇到的最初错误是

Support for the experimental Syntax ''decorators-legacy'' isn''t currently enabled

所以我把它添加到了我的 babel.config.json 中:

"plugins": [
  [
    "@babel/plugin-proposal-decorators",{
      "legacy": true
    }
  ]
]

我得到的下一个错误是

Unexpected token,expected "{"
........
node_modules/@nrwl/web/src/utils/web-babel-loader.js)

在我所有的 TypeScript 文件中。我假设 babel 需要 JavaScript,所以我添加了

"presets": [
  "@babel/preset-typescript"
],

到我的 babel.config.json。现在我遇到的问题是任何使用类验证器装饰器的类(@IsDefined()、@IsNotNull() 等...)

Module parse Failed: Unexpected token (10:8)
File was processed with these loaders:
 * ../../../node_modules/@nrwl/web/src/utils/web-babel-loader.js
You may need an additional loader to handle the result of these loaders.

我正在使用 NX 工作区,这是一个普通的 TypeScript 库……我不是 100% 确定这里出了什么问题。当我用这个库运行我的 nestJS 服务器时,它编译并运行良好。但是由于某种原因,当我运行 ReactJS 应用程序时,它失败并显示此错误。

非常感谢任何帮助!

编辑:

工作 babel.config.json

{
  "babelrcRoots": [
    "*"
  ],"presets": [
    "@babel/preset-typescript","@babel/preset-env"
  ],"plugins": [
    [
      "@babel/plugin-proposal-decorators",{
        "legacy": true
      }
    ],[
      "@babel/plugin-proposal-class-properties",[
      "@babel/plugin-transform-runtime"
    ]
  ]
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

我们今天的关于NestJS类验证器不验证数据struts2验证器的分享已经告一段落,感谢您的关注,如果您想了解更多关于Angular 2表单验证,minLength验证器不起作用、asp.net – 正则表达式验证器不验证空文本框、asp.net-mvc – 自定义模型绑定器不验证模型、Babel 在 NX 库中使用类验证器失败的相关信息,请在本站查询。

本文标签: