gpt4 book ai didi

javascript - 如何在必需和无效时显示不同的消息?

转载 作者:行者123 更新时间:2023-11-28 08:01:33 24 4
gpt4 key购买 nike

你能告诉我如何在必需和无效时显示不同的消息吗?换句话说,使用插件从 json 制作一个表单。其中有一些必需的参数。还有一些我需要验证示例“电子邮件”。当如果用户没有填写该字段,则用户按“提交”按钮,它会显示“此字段为必填”。如果用户填写了值,但模式不同,则会显示“无效值”。我们可以根据不同的情况显示不同的消息吗?

我用过这个插件 https://github.com/Textalk/angular-schema-form/blob/master/docs/index.md#validation-messages我做了这个笨蛋 http://plnkr.co/edit/ZNJO3x3IqajjdMNStJMF?p=preview

angular.module('test',['schemaForm']).controller('FormController', function($scope,$http){
$scope.schema = {
type: "object",
properties: {
name: { type: "string", minLength: 2, title: "Name", description: "Name or alias" ,required:true,"default": "dddd"},
"student": { type: "string", title: "studentname", description: "Name or student" ,required:false},

"email": {
"title": "Email",
"type": "string",
"pattern":"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",

"description": "Email will be used for evil.",
required:true
},
title: {
type: "string",
required:true,
enum: ['dr','jr','sir','mrs','mr','NaN','dj'],

}
}
};

$scope.form = [
"*",
{
type: "submit",
title: "Save"
}
];

最佳答案

正如您可能从 official angular-schema-form documentation 中读到的那样

Per default all error messages comes from the schema validator tv4, this might or might not work for you. If you supply a validationMessage property in the form definition, and if its value is a string that will be used instead on any validation error.

If you need more fine grained control you can supply an object instead with keys matching the error codes of tv4. See tv4.errorCodes

Ex.

{
key: "address.street",
validationMessage: {
tv4.errorCodes.STRING_LENGTH_SHORT: "Address is too short, man.",
"default": "Just write a proper address, will you?" //Special catch all error message
}
}

You can also set a global validationMessage in formDefaults see Global Options.

因此,要了解更多错误消息,请访问此 link

关于javascript - 如何在必需和无效时显示不同的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25330571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com